Add spring and fix controller specs

This commit is contained in:
2014-07-14 14:09:35 +02:00
parent 96841116c5
commit 6420b428a7
13 changed files with 147 additions and 185 deletions
@@ -1,27 +1,27 @@
# encoding: UTF-8
require 'spec_helper'
describe Suppliers::SectionsController do
describe Suppliers::SectionsController, type: :controller do
before :each do
@supplier = Supplier.find_by_email('supplier@qwaiter.com') || create(:supplier, :confirmed)
controller.stub(:section_params){ controller.params.require(:section).permit! } # allow all parameters since cross parameter injection is tested
sign_in @supplier
end
describe "GET #index" do
it "populates an array of sections" do
base_section = @supplier.sections.first
section = create :section, supplier: @supplier
get :index
assigns(:sections).should =~[base_section, section].compact
end
def valid_attributes
{title: 'Terrace'}
end
def invalid_attributes
{title: ''}
end
describe "GET #index" do
it "does not include sections from another supplier" do
base_section = @supplier.sections.first
section1 = create :section, supplier: @supplier
section2 = create :section
get :index
assigns(:sections).should =~[base_section, section1].compact
assigns(:sections).map(&:id).sort.should == [base_section.id, section1.id].sort
end
it "should render without errors when no objects are present" do
@@ -71,31 +71,31 @@ describe Suppliers::SectionsController do
context "with valid attributes" do
it "creates a new section" do
expect{
post :create, section: attributes_for(:section, supplier: @supplier)
}.to change(Section, :count).by(2)
post :create, section: valid_attributes
}.to change(Section, :count).by(1)
end
it "redirects to the new section" do
post :create, section: attributes_for(:section, title: 'Created section 45', supplier: @supplier)
post :create, section: valid_attributes.merge(title: 'Created section 45')
response.should redirect_to [:suppliers, Section.find_by_title('Created section 45')]
end
it "should not be possible to create a section for another supplier" do
supplier2 = create :supplier
post :create, section: attributes_for(:section, title: 'Trying to hack', supplier: supplier2)
Section.find_by_title('Trying to hack').supplier_id.should == @supplier.id
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
end
end
context "with invalid attributes" do
it "does not save the new section" do
expect{
post :create, section: {title: ''}
post :create, section: invalid_attributes
}.to_not change(Section, :count)
end
it "re-renders the new method" do
post :create, section: {title: ''}
post :create, section: invalid_attributes
response.should render_template :new
end
end
@@ -108,25 +108,27 @@ describe Suppliers::SectionsController do
context "valid attributes" do
it "located the requested section" do
put :update, id: @section, section: attributes_for(:section, supplier: @supplier)
put :update, id: @section, section: valid_attributes
@section.reload
assigns(:section).should eq(@section)
end
it "changes @section's attributes" do
put :update, id: @section, section: attributes_for(:section, title: "ChangedByTest", supplier: @supplier)
put :update, id: @section, section: valid_attributes.merge(title: "ChangedByTest")
@section.reload
@section.title.should eq("ChangedByTest")
expect( @section.title ).to eq "ChangedByTest"
end
it "redirects to the updated section" do
put :update, id: @section, section: attributes_for(:section, supplier: @supplier)
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
put :update, id: @section, section: attributes_for(:section, title: "Trying to hack", supplier: supplier2)
Section.find_by_title('Trying to hack').supplier_id.should == @supplier.id
expect{ put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id) }.to raise_error
@section.reload
@section.supplier_id.should == @supplier.id
end
it "should not be possible to update a section of another supplier" do
@@ -139,12 +141,12 @@ describe Suppliers::SectionsController do
context "invalid attributes" do
it "locates the requested section" do
put :update, id: @section, section: {title: ''}
put :update, id: @section, section: invalid_attributes
assigns(:section).should eq(@section)
end
it "re-renders the edit method" do
put :update, id: @section, section: {title: ''}
put :update, id: @section, section: invalid_attributes
response.should render_template :edit
end
end