Controller spec api improvements

This commit is contained in:
2014-12-02 19:24:09 +01:00
parent 60e6b2a648
commit a0774caeff
7 changed files with 61 additions and 164 deletions
@@ -20,46 +20,23 @@ describe Suppliers::SectionsController, type: :controller do
base_section = @supplier.sections.first
section1 = create :section, supplier: @supplier
section2 = create :section
get :index
assigns(:sections).map(&:id).sort.should == [base_section.id, section1.id].sort
end
it "should render without errors when no objects are present" do
get :index
expect{ render_template :index }.not_to raise_error
get :index, format: :json
assigns(:sections).should =~ [base_section, section1]
end
end
describe "GET #show" do
it "assigns the requested section to @section" do
section = create :section, supplier: @supplier
get :show, id: section
get :show, id: section.id, format: :json
assigns(:section).should eq(section)
end
it "should not display a section of another supplier" do
section = create :section
get :show, id: section
get :show, id: section, format: :json
response.status.should == 404
end
it "renders the #show view" do
section = create :section, supplier: @supplier
get :show, id: section
response.should render_template :tables_view
end
end
describe "GET #new" do
it "assigns a new section to @section" do
get :new
assigns(:section).should be_a Section
end
it "renders the #show view" do
get :new
response.should render_template :new
end
end
describe "POST #create" do
@@ -68,30 +45,26 @@ describe Suppliers::SectionsController, type: :controller do
expect{
post :create, section: valid_attributes
}.to change(Section, :count).by(1)
end
it "redirects to the new section" do
post :create, section: valid_attributes.merge(title: 'Created section 45')
response.should redirect_to [:suppliers, Section.find_by_title('Created section 45')]
expect( Section.last.supplier_id ).to eq @supplier.id
end
it "should not be possible to create a section for another supplier" do
supplier2 = create :supplier
expect{ post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error
expect( Section.find_by_title 'Trying to hack' ).not_to be_present
post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id)
expect( Section.find_by_title('Trying to hack').supplier_id ).to eq @supplier.id
end
end
context "with invalid attributes" do
it "does not save the new section" do
expect{
post :create, section: invalid_attributes
post :create, format: :json, section: invalid_attributes
}.to_not change(Section, :count)
end
it "re-renders the new method" do
post :create, section: invalid_attributes
response.should render_template :new
it "returns an error response" do
post :create, format: :json, section: invalid_attributes
expect( JSON.parse(response.body )['errors'] ).to be_present
end
end
end
@@ -103,32 +76,28 @@ describe Suppliers::SectionsController, type: :controller do
context "valid attributes" do
it "located the requested section" do
put :update, id: @section, section: valid_attributes
put :update, id: @section, format: :json, section: valid_attributes
@section.reload
assigns(:section).should eq(@section)
end
it "changes @section's attributes" do
put :update, id: @section, section: valid_attributes.merge(title: "ChangedByTest")
put :update, id: @section.id, format: :json, section: valid_attributes.merge(title: "ChangedByTest")
@section.reload
expect( @section.title ).to eq "ChangedByTest"
end
it "redirects to the updated section" do
put :update, id: @section, section: valid_attributes
response.should redirect_to [:suppliers, @section]
end
it "should not be possible to update a section to another supplier" do
supplier2 = create :supplier
expect{ put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id) }.to raise_error
put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id)
@section.reload
@section.supplier_id.should == @supplier.id
end
it "should not be possible to update a section of another supplier" do
section = create :section, title: 'Other supplier section'
put :update, id: section, section: {title: "Trying to hack"}
put :update, id: section.id, format: :json, section: {title: "Trying to hack"}
expect( response ).to be_not_found
section.reload
section.title.should == 'Other supplier section'
end
@@ -136,13 +105,13 @@ describe Suppliers::SectionsController, type: :controller do
context "invalid attributes" do
it "locates the requested section" do
put :update, id: @section, section: invalid_attributes
put :update, id: @section, format: :json, section: invalid_attributes
assigns(:section).should eq(@section)
end
it "re-renders the edit method" do
put :update, id: @section, section: invalid_attributes
response.should render_template :edit
it "returns an error response" do
put :update, id: @section.id, format: :json, section: invalid_attributes
expect( JSON.parse(response.body )['errors'] ).to be_present
end
end
end
@@ -158,11 +127,6 @@ describe Suppliers::SectionsController, type: :controller do
}.to change(Section, :count).by(-1)
end
it "redirects to sections#index" do
delete :destroy, id: @section
response.should redirect_to [:suppliers, :sections]
end
it "should not be possible to delete a section of another supplier" do
section = create :section
expect{