More spec fixing

This commit is contained in:
2014-12-04 19:00:53 +01:00
parent 524140cf60
commit 3941529d0d
6 changed files with 46 additions and 38 deletions
@@ -23,10 +23,6 @@ describe Suppliers::ProductCategoriesController, type: :controller do
assigns(:product_categories).should eq([product_category1])
end
it "should render without errors when no objects are present" do
get :index
expect{ render_template :index }.not_to raise_error
end
end
describe "GET #show" do
@@ -65,8 +61,8 @@ describe Suppliers::ProductCategoriesController, type: :controller do
it "should not be possible to create a product category for another supplier" do
supplier2 = create :supplier
expect{ post :create, product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error
ProductCategory.find_by_name('Trying to hack').should_not be_present
post :create, product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id)
expect( ProductCategory.find_by_name('Trying to hack').supplier_id ).to eq @supplier.id
end
end
@@ -76,6 +72,11 @@ describe Suppliers::ProductCategoriesController, type: :controller do
post :create, product_category: invalid_attributes
}.not_to change{ ProductCategory.count }
end
it 'returns an error object' do
post :create, format: :json, product_category: invalid_attributes
expect( JSON.parse(response.body)['errors'] ).to be_present
end
end
end
@@ -99,10 +100,10 @@ describe Suppliers::ProductCategoriesController, type: :controller do
it "should not be possible to update a product category to another supplier" do
supplier2 = create :supplier
expect{
put :update, id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
}.to raise_error
ProductCategory.find_by_name('Trying to hack').should_not be_present
put :update, id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
@product_category.reload
expect( @product_category.name ).to eq 'Trying to hack'
expect( @product_category.supplier_id ).to eq @supplier.id
end
it "should not be possible to update a product_category of another supplier" do
@@ -113,6 +114,13 @@ describe Suppliers::ProductCategoriesController, type: :controller do
product_category.name.should == 'Other supplier product_category'
end
end
context "with invalid attributes" do
it "returns an error response" do
put :update, id: @product_category, format: :json, product_category: invalid_attributes
expect( JSON.parse( response.body )['errors']).to be_present
end
end
end
describe 'DELETE destroy' do
@@ -22,11 +22,6 @@ describe Suppliers::ProductsController, type: :controller do
get :index
assigns(:products).should eq([product1])
end
it "should render without errors when no objects are present" do
get :index
expect{ render_template :index }.not_to raise_error
end
end
describe "GET #show" do
@@ -65,8 +60,8 @@ describe Suppliers::ProductsController, type: :controller do
it "should not be possible to create a product for another supplier" do
supplier2 = create :supplier
expect{ post :create, product: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error
Product.find_by_name('Trying to hack').should_not be_present
post :create, product: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id)
expect( Product.find_by_name('Trying to hack').supplier_id ).to eq @supplier.id
end
end
@@ -77,9 +72,9 @@ describe Suppliers::ProductsController, type: :controller do
}.to_not change(Product, :count)
end
it "re-renders the new method" do
post :create, product: invalid_attributes
response.should render_template :new
it 'returns an error object' do
post :create, format: :json, product: invalid_attributes
expect( JSON.parse(response.body)['errors'] ).to be_present
end
end
end
@@ -104,27 +99,22 @@ describe Suppliers::ProductsController, type: :controller do
it "should not be possible to update a product to another supplier" do
supplier2 = create :supplier
expect{ put :update, id: @product, product: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id) }.to raise_error
expect( Product.find_by_name 'Trying to hack' ).not_to be_present
put :update, id: @product, product: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
expect( Product.find_by_name('Trying to hack' ).supplier_id ).to eq @supplier.id
end
it "should not be possible to update a product of another supplier" do
product = create :product, name: 'Other supplier product'
put :update, id: product, product: {name: "Trying to hack"}
product.reload
product.name.should == 'Other supplier product'
expect( product.name ).to eq 'Other supplier product'
end
end
context "invalid attributes" do
it "locates the requested product" do
put :update, id: @product, product: invalid_attributes
assigns(:product).should eq(@product)
end
it "re-renders the edit method" do
put :update, id: @product, product: invalid_attributes
response.should render_template :edit
context "with invalid attributes" do
it "returns an error response" do
put :update, id: @product, format: :json, product: invalid_attributes
expect( JSON.parse( response.body )['errors']).to be_present
end
end
end