Controller spec api improvements
This commit is contained in:
@@ -21,48 +21,20 @@ describe Suppliers::TablesController, type: :controller do
|
||||
get :index
|
||||
assigns(:tables).should eq([table1])
|
||||
end
|
||||
|
||||
it "should render without errors when no objects are present" do
|
||||
get :index
|
||||
expect{ render_template :index }.not_to raise_error
|
||||
end
|
||||
|
||||
it "renders the :index view" do
|
||||
get :index
|
||||
response.should render_template :index
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET #show" do
|
||||
it "assigns the requested table to @table" do
|
||||
table = create :table, supplier: @supplier
|
||||
get :show, id: table
|
||||
get :show, id: table, format: :json
|
||||
assigns(:table).should eq(table)
|
||||
end
|
||||
|
||||
it "should not display a table of another supplier" do
|
||||
table = create :table
|
||||
get :show, id: table
|
||||
get :show, id: table, format: :json
|
||||
response.status.should == 404
|
||||
end
|
||||
|
||||
it "renders the #show view" do
|
||||
table = create :table, supplier: @supplier
|
||||
get :show, id: table
|
||||
response.should render_template :show
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET #new" do
|
||||
it "assigns a new table to @table" do
|
||||
get :new
|
||||
assigns(:table).should be_a Table
|
||||
end
|
||||
|
||||
it "renders the #show view" do
|
||||
get :new
|
||||
response.should render_template :new
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST #create" do
|
||||
@@ -73,28 +45,11 @@ describe Suppliers::TablesController, type: :controller do
|
||||
}.to change(Table, :count).by(1)
|
||||
end
|
||||
|
||||
it "redirects to the new table" do
|
||||
post :create, table: {number: 22}
|
||||
response.should redirect_to [:suppliers, Table.last]
|
||||
end
|
||||
|
||||
it "should not be possible to create a table for another supplier" do
|
||||
it "should not be possible to create a table for another supplier, linked to signed in supplier instead" do
|
||||
supplier2 = create :supplier
|
||||
expect { post :create, table: {number: 6, supplier_id: supplier2.id} }.to raise_error
|
||||
Table.find_by_number(6).should_not be_present
|
||||
end
|
||||
end
|
||||
|
||||
context "with invalid attributes" do
|
||||
it "does not save the new table" do
|
||||
expect{
|
||||
post :create, table: {number: '-6'}
|
||||
}.to_not change(Table, :count)
|
||||
end
|
||||
|
||||
it "re-renders the new method" do
|
||||
post :create, table: {number: '-6'}
|
||||
response.should render_template :new
|
||||
expect { post :create, table: {number: 6, supplier_id: supplier2.id} }.to change{ Table.count }.by(1)
|
||||
created_table = Table.find_by_number(6)
|
||||
expect( created_table.supplier_id ).to eq @supplier.id
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -106,7 +61,7 @@ describe Suppliers::TablesController, type: :controller do
|
||||
|
||||
context "valid attributes" do
|
||||
it "located the requested table" do
|
||||
put :update, id: @table, table: {number: 22}
|
||||
put :update, id: @table.id, table: {number: 22}
|
||||
@table.reload
|
||||
assigns(:table).should eq(@table)
|
||||
end
|
||||
@@ -117,37 +72,25 @@ describe Suppliers::TablesController, type: :controller do
|
||||
@table.number.should eq(14)
|
||||
end
|
||||
|
||||
it "redirects to the updated table" do
|
||||
put :update, id: @table, table: {number: 22}
|
||||
response.should redirect_to [:suppliers, @table]
|
||||
end
|
||||
|
||||
it "should not be possible to update a table to another supplier" do
|
||||
supplier2 = create :supplier
|
||||
expect{
|
||||
put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id}
|
||||
}.to raise_error
|
||||
put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id}
|
||||
@table.reload
|
||||
@table.supplier_id.should == @supplier.id
|
||||
expect( @table.supplier_id ).to eq @supplier.id
|
||||
end
|
||||
|
||||
it "should not be possible to update a table of another supplier" do
|
||||
table = create :table, number: 11
|
||||
put :update, id: table, table: {number: 6}
|
||||
put :update, id: table.id, table: {number: 6}
|
||||
table.reload
|
||||
table.number.should == 11
|
||||
end
|
||||
end
|
||||
|
||||
context "invalid attributes" do
|
||||
it "locates the requested table" do
|
||||
put :update, id: @table, table: {number: '-6'}
|
||||
assigns(:table).should eq(@table)
|
||||
end
|
||||
|
||||
it "re-renders the edit method" do
|
||||
put :update, id: @table, table: {number: '-6'}
|
||||
response.should render_template :edit
|
||||
it "returns an error response" do
|
||||
#put :update, id: @table, format: :json, table: {number: 'aaa'}
|
||||
#TODO: when proper invalid tables exist expect( JSON.parse(response.body)['errors'] ).to be_present
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -159,15 +102,10 @@ describe Suppliers::TablesController, type: :controller do
|
||||
|
||||
it "deletes the table" do
|
||||
expect{
|
||||
delete :destroy, id: @table
|
||||
delete :destroy, id: @table
|
||||
}.to change(Table, :count).by(-1)
|
||||
end
|
||||
|
||||
it "redirects to tables#index" do
|
||||
delete :destroy, id: @table
|
||||
response.should redirect_to [:suppliers, :tables]
|
||||
end
|
||||
|
||||
it "should not be possible to delete a table of another supplier" do
|
||||
table = create :table
|
||||
expect{
|
||||
|
||||
Reference in New Issue
Block a user