make more specs work

This commit is contained in:
2025-09-23 17:45:00 -05:00
parent 6596693238
commit 7dadb28004
7 changed files with 84 additions and 52 deletions
@@ -38,19 +38,19 @@ describe Suppliers::ListsController, type: :controller do
describe "GET #show" do
it "assigns the requested list to @list" do
list = create :list, supplier: @supplier
get :show, id: list
get :show, params: {id: list.id}
assigns(:list).should eq(list)
end
it "should not display a list of another supplier" do
list = create :list
get :show, id: list
get :show, params: {id: list.id}
response.status.should == 404
end
it "renders the #show view" do
list = create :list, supplier: @supplier
get :show, id: list
get :show, params: {id: list.id}
response.body.should include 'orders'
end
end
@@ -66,13 +66,13 @@ describe Suppliers::ListsController, type: :controller do
context "with valid attributes" do
it "creates a new list" do
expect{
post :create, list: valid_attributes
post :create, params: {list: valid_attributes}
}.to change(List, :count).by(1)
end
it "should not be possible to create a list for another supplier" do
supplier2 = create :supplier
post :create, list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id)
post :create, params: {list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id)}
expect( List.find_by_price(6.66).supplier_id ).to eq @supplier.id
end
end
@@ -80,13 +80,13 @@ describe Suppliers::ListsController, type: :controller do
context "with invalid attributes", broken: true do
it "does not save the new list" do
expect{
post :create, format: :json, list: invalid_params
post :create, params: {list: invalid_params}, format: :json
}.not_to change{ List.count }
List.count.should be_zero
end
it 'returns an error object' do
post :create, format: :json, list: invalid_params
post :create, params: {list: invalid_params}, format: :json
expect( JSON.parse(response.body)['errors'] ).to be_present
end
end
@@ -99,27 +99,27 @@ describe Suppliers::ListsController, type: :controller do
context "valid attributes" do
it "located the requested list" do
put :update, id: @list, list: valid_attributes
put :update, params: {id: @list.id, list: valid_attributes}
@list.reload
assigns(:list).should eq(@list)
end
it "changes @list's price attribute" do
put :update, id: @list, list: valid_attributes.merge(price: '7.22')
put :update, params: {id: @list.id, list: valid_attributes.merge(price: '7.22')}
@list.reload
@list.price.should eq(7.22)
end
it "should not be possible to update a list to another supplier" do
supplier2 = create :supplier
put :update, id: @list, list: valid_attributes.merge(supplier_id: supplier2.id)
put :update, params: {id: @list.id, list: valid_attributes.merge(supplier_id: supplier2.id)}
@list.reload
@list.supplier_id.should == @supplier.id
end
it "should not be possible to update a list of another supplier" do
list = create :list, price: '7.22'
put :update, id: list, list: {price: '6.66'}
put :update, params: {id: list.id, list: {price: '6.66'}}
list.reload
list.price.should == 7.22
end
@@ -127,7 +127,7 @@ describe Suppliers::ListsController, type: :controller do
context "invalid attributes", broken: true do
it "returns an error response" do
put :update, id: @list, format: :json, list: invalid_params
put :update, params: {id: @list, list: invalid_params}, format: :json
expect( JSON.parse( response.body )['errors']).to be_present
end
end
@@ -140,14 +140,14 @@ describe Suppliers::ListsController, type: :controller do
it "deletes the list" do
expect{
delete :destroy, id: @list
delete :destroy, params: {id: @list.id}
}.to change(List, :count).by(-1)
end
it "should not be possible to delete a list of another supplier" do
list = create :list
expect{
delete :destroy, id: list
delete :destroy, params: {id: list.id}
}.to_not change(List, :count)
end
end
@@ -27,13 +27,13 @@ describe Suppliers::ProductCategoriesController, type: :controller do
describe "GET #show" do
it "assigns the requested product_category to @product_category" do
product_category = create :product_category, supplier: @supplier
get :show, id: product_category, format: :json
get :show, params: {id: product_category}, format: :json
assigns(:product_category).should eq(product_category)
end
it "should not display a product_category of another supplier" do
product_category = create :product_category
get :show, id: product_category, format: :json
get :show, params: {id: product_category}, format: :json
response.status.should == 404
end
end
@@ -42,18 +42,18 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "with valid attributes" do
it "creates a new product_category" do
expect{
post :create, product_category: valid_attributes, format: :json
post :create, params: {product_category: valid_attributes}, format: :json
}.to change(ProductCategory, :count).by(1)
end
it "redirects to the new product_category" do
post :create, product_category: valid_attributes
post :create, params: {product_category: valid_attributes}
response.should redirect_to [:suppliers, :product_categories]
end
it "should not be possible to create a product category for another supplier" do
supplier2 = create :supplier
post :create, product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id)
post :create, params: {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
@@ -61,12 +61,12 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "with invalid attributes" do
it "does not save the new product_category" do
expect{
post :create, product_category: invalid_attributes, format: :json
post :create, params: {product_category: invalid_attributes}, format: :json
}.not_to change{ ProductCategory.count }
end
it 'returns an error object' do
post :create, format: :json, product_category: invalid_attributes, format: :json
post :create, params: {product_category: invalid_attributes}, format: :json
expect( JSON.parse(response.body)['errors'] ).to be_present
end
end
@@ -79,20 +79,20 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "valid attributes" do
it "located the requested product_category" do
put :update, id: @product_category, product_category: valid_attributes
put :update, params: {id: @product_category, product_category: valid_attributes}
@product_category.reload
assigns(:product_category).should eq(@product_category)
end
it "changes @product_category's attributes" do
put :update, id: @product_category, product_category: valid_attributes.merge(name: "ChangedByTest")
put :update, params: {id: @product_category, product_category: valid_attributes.merge(name: "ChangedByTest")}
@product_category.reload
@product_category.name.should eq("ChangedByTest")
end
it "should not be possible to update a product category to another supplier" do
supplier2 = create :supplier
put :update, id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
put :update, params: {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
@@ -100,7 +100,7 @@ describe Suppliers::ProductCategoriesController, type: :controller do
it "should not be possible to update a product_category of another supplier" do
product_category = create :product_category, name: 'Other supplier product_category'
put :update, id: product_category, product_category: {name: "Trying to hack"}
put :update, params: {id: product_category, product_category: {name: "Trying to hack"}}
expect(response.status).to eq 404
product_category.reload
product_category.name.should == 'Other supplier product_category'
@@ -109,7 +109,7 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "with invalid attributes" do
it "returns an error response" do
put :update, id: @product_category, format: :json, product_category: invalid_attributes
put :update, params: {id: @product_category, product_category: invalid_attributes}, format: :json
expect( JSON.parse( response.body )['errors']).to be_present
end
end
@@ -122,14 +122,14 @@ describe Suppliers::ProductCategoriesController, type: :controller do
it "deletes the product_category" do
expect{
delete :destroy, id: @product_category
delete :destroy, params: {id: @product_category.id}
}.to change(ProductCategory, :count).by(-1)
end
it "should not be possible to delete a product category of another supplier" do
product_category = create :product_category
expect{
delete :destroy, id: product_category
delete :destroy, params: {id: product_category.id}
}.to_not change{ProductCategory.count }
end
end
@@ -27,13 +27,13 @@ describe Suppliers::SectionsController, type: :controller do
describe "GET #show" do
it "assigns the requested section to @section" do
section = create :section, supplier: @supplier
get :show, id: section.id, format: :json
get :show, params: {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, format: :json
get :show, params: {id: section.id}, format: :json
response.status.should == 404
end
end
@@ -42,14 +42,14 @@ describe Suppliers::SectionsController, type: :controller do
context "with valid attributes" do
it "creates a new section" do
expect{
post :create, section: valid_attributes
post :create, params: {section: valid_attributes}
}.to change(Section, :count).by(1)
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
post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id)
post :create, params: {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
@@ -57,12 +57,12 @@ describe Suppliers::SectionsController, type: :controller do
context "with invalid attributes" do
it "does not save the new section" do
expect{
post :create, format: :json, section: invalid_attributes
post :create, params: {section: invalid_attributes}, format: :json
}.to_not change(Section, :count)
end
it "returns an error response" do
post :create, format: :json, section: invalid_attributes
post :create, params: {section: invalid_attributes}, format: :json
expect( JSON.parse(response.body )['errors'] ).to be_present
end
end
@@ -75,27 +75,27 @@ describe Suppliers::SectionsController, type: :controller do
context "valid attributes" do
it "located the requested section" do
put :update, id: @section, format: :json, section: valid_attributes
put :update, params: {id: @section, section: valid_attributes}, format: :json
@section.reload
assigns(:section).should eq(@section)
end
it "changes @section's attributes" do
put :update, id: @section.id, format: :json, section: valid_attributes.merge(title: "ChangedByTest")
put :update, params: {id: @section.id, section: valid_attributes.merge(title: "ChangedByTest")}, format: :json
@section.reload
expect( @section.title ).to eq "ChangedByTest"
end
it "should not be possible to update a section to another supplier" do
supplier2 = create :supplier
put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id)
put :update, params: {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.id, format: :json, section: {title: "Trying to hack"}
put :update, params: {id: section.id, section: {title: "Trying to hack"}}, format: :json
expect( response ).to be_not_found
section.reload
section.title.should == 'Other supplier section'
@@ -104,12 +104,12 @@ describe Suppliers::SectionsController, type: :controller do
context "invalid attributes" do
it "locates the requested section" do
put :update, id: @section, format: :json, section: invalid_attributes
put :update, params: {id: @section, section: invalid_attributes}, format: :json
assigns(:section).should eq(@section)
end
it "returns an error response" do
put :update, id: @section.id, format: :json, section: invalid_attributes
put :update, params: {id: @section.id, section: invalid_attributes}, format: :json
expect( JSON.parse(response.body )['errors'] ).to be_present
end
end
@@ -122,14 +122,14 @@ describe Suppliers::SectionsController, type: :controller do
it "deletes the section" do
expect{
delete :destroy, id: @section
delete :destroy, params: {id: @section.id}
}.to change(Section, :count).by(-1)
end
it "should not be possible to delete a section of another supplier" do
section = create :section
expect{
delete :destroy, id: section
delete :destroy, params: {id: section.id}
}.to_not change(Section, :count)
end
end
@@ -24,7 +24,7 @@ describe Suppliers::TablesController, type: :controller do
describe "GET #show" do
it "assigns the requested table to @table" do
table = create :table, supplier: @supplier
get :show, params: {id: table}, format: :json
get :show, params: {id: table.id}, format: :json
assigns(:table).should eq(table)
end