diff --git a/Gemfile b/Gemfile index 93f010e2..fb5abbca 100644 --- a/Gemfile +++ b/Gemfile @@ -72,35 +72,37 @@ gem 'mini_magick' # To use Jbuilder templates for JSON # gem 'jbuilder' +# +group :development, :test do + gem 'rspec-rails' + gem 'pry-rails' +end group :development do #gem 'pry-remote' - gem 'rspec-rails' gem 'quiet_assets' gem 'letter_opener' gem 'thin' gem 'faye' - gem 'pry-rails' gem 'capistrano', '~> 3.0', require: false gem 'capistrano-rvm', '~> 0.1', require: false gem 'capistrano-rails', '~> 1.1', require: false - #gem 'spring' - #gem 'spring-commands-rspec' + gem 'spring' + gem 'spring-commands-rspec' end group :test do #gem 'steak' - gem 'rspec-rails' gem 'database_cleaner' gem 'capybara' #, '2.0.3' #gem 'selenium-webdriver' gem 'capybara-webkit' #, '~>0.14.2' # version 1.1.0 does not yet compile in mavericks gem 'turnip' + gem 'rspec-its' gem 'launchy' gem 'fuubar' #gem 'rb-fsevent', :require => false #if RUBY_PLATFORM =~ /darwin/i gem 'simplecov', require: false gem 'factory_girl_rails' - gem 'pry-rails' end # Use unicorn as the app server diff --git a/Gemfile.lock b/Gemfile.lock index 524f9f78..4e09eff1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -37,7 +37,7 @@ GIT GIT remote: git://github.com/bterkuile/simply_stored.git - revision: a4b672f68cbcf275aa10fc5014a3763238691956 + revision: e0e72e8e73c59666191d578408572ed8f5de9e42 specs: simply_stored (1.0.0) activesupport @@ -322,6 +322,9 @@ GEM rspec-expectations (3.0.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.0.0) + rspec-its (1.0.1) + rspec-core (>= 2.99.0.beta1) + rspec-expectations (>= 2.99.0.beta1) rspec-mocks (3.0.2) rspec-support (~> 3.0.0) rspec-rails (3.0.1) @@ -357,6 +360,9 @@ GEM railties (>= 3.0, < 4.2) slim (~> 2.0) slop (3.5.0) + spring (1.1.3) + spring-commands-rspec (1.0.2) + spring (>= 0.9.1) sprockets (2.11.0) hike (~> 1.2) multi_json (~> 1.0) @@ -384,7 +390,7 @@ GEM treetop (1.4.15) polyglot polyglot (>= 0.3.1) - turnip (1.2.1) + turnip (1.2.2) gherkin (>= 2.5) rspec (>= 2.0, < 4.0) tzinfo (1.2.1) @@ -443,12 +449,15 @@ DEPENDENCIES rack-cors rails (= 4.1.4) rqrcode + rspec-its rspec-rails sass-rails (~> 4.0.2) simple_form simplecov simply_stored! slim-rails + spring + spring-commands-rspec thin turnip uglifier (>= 1.0.3) diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 52ee53c1..66b59480 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -1,6 +1,8 @@ class Supplier include SimplyStored::Couch include ActiveModel::SerializerSupport + + view :by_confirmation_token, key: :confirmation_token # devise confirmable devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable, :confirmable property :unconfirmed_email diff --git a/bin/rails b/bin/rails index 1c894d52..7feb6a30 100755 --- a/bin/rails +++ b/bin/rails @@ -3,8 +3,6 @@ begin load File.expand_path("../spring", __FILE__) rescue LoadError end -# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. - APP_PATH = File.expand_path('../../config/application', __FILE__) -require File.expand_path('../../config/boot', __FILE__) +require_relative '../config/boot' require 'rails/commands' diff --git a/bin/rspec b/bin/rspec new file mode 100755 index 00000000..20060ebd --- /dev/null +++ b/bin/rspec @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby +begin + load File.expand_path("../spring", __FILE__) +rescue LoadError +end +require 'bundler/setup' +load Gem.bin_path('rspec-core', 'rspec') diff --git a/spec/controllers/supplier/edit_spec.rb b/spec/controllers/supplier/edit_spec.rb index b293bf9e..c83a3c81 100644 --- a/spec/controllers/supplier/edit_spec.rb +++ b/spec/controllers/supplier/edit_spec.rb @@ -1,7 +1,7 @@ # encoding: UTF-8 require 'spec_helper' -describe SupplierController do +describe SupplierController, type: :controller do before :each do @supplier = Supplier.find_by_email('supplier@qwaiter.com') || Supplier.create(name: 'Supplier', email: 'supplier@qwaiter.com', password: 'secret') sign_in @supplier diff --git a/spec/controllers/suppliers/lists_controller_spec.rb b/spec/controllers/suppliers/lists_controller_spec.rb index c4aa1de6..9c5e331f 100644 --- a/spec/controllers/suppliers/lists_controller_spec.rb +++ b/spec/controllers/suppliers/lists_controller_spec.rb @@ -1,25 +1,27 @@ # encoding: UTF-8 require 'spec_helper' -describe Suppliers::ListsController do +describe Suppliers::ListsController, type: :controller do before :each do @supplier = Supplier.find_by_email('supplier@qwaiter.com') || create(:supplier, :confirmed) ActionController::Parameters.permit_all_parameters = false - controller.stub(:list_params){ controller.params.require(:list).permit! } # allow all parameters since cross parameter injection is tested + #controller.stub(:list_params){ controller.params.require(:list).permit! } # allow all parameters since cross parameter injection is tested sign_in @supplier end + def valid_attributes + {state: 'active'} + end + + def invalid_params + {foo: 'bar'} + end + #after :each do #ActionController::Parameters.permit_all_parameters = true #end describe "GET #index" do - it "populates an array of lists" do - list = create :list, supplier: @supplier - get :index - assigns(:lists).should eq([list]) - end - it "has pagination options if show_all is given" do list = create :list, supplier: @supplier get :index, show_all: 'yes' @@ -81,32 +83,28 @@ describe Suppliers::ListsController do context "with valid attributes" do it "creates a new list" do expect{ - post :create, list: attributes_for(:list, supplier: @supplier) + post :create, list: valid_attributes }.to change(List, :count).by(1) end it "redirects to the new list" do - post :create, list: attributes_for(:list, supplier: @supplier) + post :create, list: valid_attributes response.should redirect_to [:suppliers, List.last] end it "should not be possible to create a list for another supplier" do supplier2 = create :supplier - post :create, list: attributes_for(:list, price: '6.66', supplier: supplier2) - List.find_by_price(6.66).supplier_id.should == @supplier.id + expect{ post :create, list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id) }.to raise_error + List.find_by_price(6.66).should_not be_present end end context "with invalid attributes" do it "does not save the new list" do expect{ - post :create, list: {table_id: ''} - }.to_not change(List, :count) - end - - it "re-renders the new method" do - post :create, list: {table_id: ''} - response.should render_template :new + post :create, list: invalid_params + }.to raise_error + List.count.should be_zero end end end @@ -118,24 +116,25 @@ describe Suppliers::ListsController do context "valid attributes" do it "located the requested list" do - put :update, id: @list, list: attributes_for(:list, supplier: @supplier) + put :update, id: @list, list: valid_attributes @list.reload assigns(:list).should eq(@list) end - it "changes @list's attributes" do - put :update, id: @list, list: attributes_for(:list, price: "7.22", supplier: @supplier) + it "changes @list's price attribute" do + put :update, id: @list, list: valid_attributes.merge(price: '7.22') @list.reload @list.price.should eq(7.22) end it "redirects to the updated list" do - put :update, id: @list, list: attributes_for(:list, supplier: @supplier) + put :update, id: @list, list: valid_attributes response.should redirect_to [:suppliers, @list] end + it "should not be possible to update a list to another supplier" do supplier2 = create :supplier - put :update, id: @list, list: attributes_for(:list, supplier: supplier2) + expect{ put :update, id: @list, list: valid_attributes.merge(supplier_id: supplier2.id) }.to raise_error @list.reload @list.supplier_id.should == @supplier.id end @@ -149,14 +148,8 @@ describe Suppliers::ListsController do end context "invalid attributes" do - it "locates the requested list" do - put :update, id: @list, list: {table_id: ''} - assigns(:list).should eq(@list) - end - - it "re-renders the edit method" do - put :update, id: @list, list: {table_id: ''} - response.should render_template :edit + it "raises on invalid params" do + expect{ put :update, id: @list, list: invalid_params }.to raise_error end end end @@ -168,7 +161,7 @@ describe Suppliers::ListsController do it "deletes the list" do expect{ - delete :destroy, id: @list + delete :destroy, id: @list }.to change(List, :count).by(-1) end diff --git a/spec/controllers/suppliers/product_categories_controller_spec.rb b/spec/controllers/suppliers/product_categories_controller_spec.rb index 41e0dc76..cc0b5588 100644 --- a/spec/controllers/suppliers/product_categories_controller_spec.rb +++ b/spec/controllers/suppliers/product_categories_controller_spec.rb @@ -1,20 +1,21 @@ # encoding: UTF-8 require 'spec_helper' -describe Suppliers::ProductCategoriesController do +describe Suppliers::ProductCategoriesController, type: :controller do before :each do @supplier = Supplier.find_by_email('supplier@qwaiter.com') || create(:supplier, :confirmed) - controller.stub(:product_category_params){ controller.params.require(:product_category).permit! } # allow all parameters since cross parameter injection is tested sign_in @supplier end - describe "GET #index" do - it "populates an array of product_categories" do - product_category = create :product_category, supplier: @supplier - get :index - assigns(:product_categories).should eq([product_category]) - end + def valid_attributes + {name: 'Lunch'} + end + def invalid_attributes + {name: ''} + end + + describe "GET #index" do it "does not include product_categories from another supplier" do product_category1 = create :product_category, supplier: @supplier product_category2 = create :product_category @@ -26,11 +27,6 @@ describe Suppliers::ProductCategoriesController 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 @@ -45,12 +41,6 @@ describe Suppliers::ProductCategoriesController do get :show, id: product_category response.status.should == 404 end - - it "renders the #show view" do - product_category = create :product_category, supplier: @supplier - get :show, id: product_category - response.should render_template :show - end end describe "GET #new" do @@ -58,43 +48,33 @@ describe Suppliers::ProductCategoriesController do get :new assigns(:product_category).should be_a ProductCategory end - - it "renders the #show view" do - get :new - response.should render_template :new - end end describe "POST #create" do context "with valid attributes" do it "creates a new product_category" do expect{ - post :create, product_category: attributes_for(:product_category, supplier: @supplier) + post :create, product_category: valid_attributes }.to change(ProductCategory, :count).by(1) end it "redirects to the new product_category" do - post :create, product_category: attributes_for(:product_category, supplier: @supplier) + post :create, 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: attributes_for(:product_category, name: 'Trying to hack', supplier: supplier2) - ProductCategory.find_by_name('Trying to hack').supplier_id.should == @supplier.id + 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 end end context "with invalid attributes" do it "does not save the new product_category" do expect{ - post :create, product_category: {name: ''} - }.to_not change(ProductCategory, :count) - end - - it "re-renders the new method" do - post :create, product_category: {name: ''} - response.should render_template :new + post :create, product_category: invalid_attributes + }.not_to change{ ProductCategory.count } end end end @@ -106,46 +86,33 @@ describe Suppliers::ProductCategoriesController do context "valid attributes" do it "located the requested product_category" do - put :update, id: @product_category, product_category: attributes_for(:product_category, supplier: @supplier) + put :update, 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: attributes_for(:product_category, name: "ChangedByTest", supplier: @supplier) + put :update, id: @product_category, product_category: valid_attributes.merge(name: "ChangedByTest") @product_category.reload @product_category.name.should eq("ChangedByTest") end - it "redirects to the updated product_category" do - put :update, id: @product_category, product_category: attributes_for(:product_category, supplier: @supplier) - response.should redirect_to [:suppliers, :product_categories] - 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: attributes_for(:product_category, name: "Trying to hack", supplier: supplier2) - ProductCategory.find_by_name('Trying to hack').supplier_id.should == @supplier.id + 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 end 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"} + expect(response.status).to eq 404 product_category.reload product_category.name.should == 'Other supplier product_category' end end - - context "invalid attributes" do - it "locates the requested product_category" do - put :update, id: @product_category, product_category: {name: ''} - assigns(:product_category).should eq(@product_category) - end - - it "re-renders the edit method" do - put :update, id: @product_category, product_category: {name: ''} - response.should render_template :edit - end - end end describe 'DELETE destroy' do @@ -155,20 +122,15 @@ describe Suppliers::ProductCategoriesController do it "deletes the product_category" do expect{ - delete :destroy, id: @product_category + delete :destroy, id: @product_category }.to change(ProductCategory, :count).by(-1) end - it "redirects to product_categories#index" do - delete :destroy, id: @product_category - response.should redirect_to [:suppliers, :product_categories] - 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 - }.to_not change(ProductCategory, :count) + }.to_not change{ProductCategory.count } end end end diff --git a/spec/controllers/suppliers/products_controller_spec.rb b/spec/controllers/suppliers/products_controller_spec.rb index ee195cdc..fbf8441a 100644 --- a/spec/controllers/suppliers/products_controller_spec.rb +++ b/spec/controllers/suppliers/products_controller_spec.rb @@ -1,20 +1,21 @@ # encoding: UTF-8 require 'spec_helper' -describe Suppliers::ProductsController do +describe Suppliers::ProductsController, type: :controller do + def valid_attributes + {name: 'Lemon cake', price: '4.2'} + end + + def invalid_attributes + {name: ''} + end + before :each do @supplier = Supplier.find_by_email('supplier@qwaiter.com') || create(:supplier, :confirmed) - controller.stub(:product_params){ controller.params.require(:product).permit! } # allow all parameters since cross parameter injection is tested sign_in @supplier end describe "GET #index" do - it "populates an array of products" do - product = create :product, supplier: @supplier - get :index - assigns(:products).should eq([product]) - end - it "does not include products from another supplier" do product1 = create :product, supplier: @supplier product2 = create :product @@ -26,11 +27,6 @@ describe Suppliers::ProductsController 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 @@ -43,22 +39,11 @@ describe Suppliers::ProductsController do it "should not display a product of another supplier" do product = create :product get :show, id: product - response.status.should == 404 - end - - it "renders the #show view" do - product = create :product, supplier: @supplier - get :show, id: product - response.should render_template :show + expect(response.status).to eq 404 end end describe "GET #new" do - it "assigns a new product to @product" do - get :new - assigns(:product).should be_a Product - end - it "renders the #show view" do get :new response.should render_template :new @@ -69,31 +54,31 @@ describe Suppliers::ProductsController do context "with valid attributes" do it "creates a new product" do expect{ - post :create, product: attributes_for(:product, supplier: @supplier) + post :create, product: valid_attributes }.to change(Product, :count).by(1) end it "redirects to the new product" do - post :create, product: attributes_for(:product, supplier: @supplier) + post :create, product: valid_attributes response.should redirect_to [:suppliers, :products] end it "should not be possible to create a product for another supplier" do supplier2 = create :supplier - post :create, product: attributes_for(:product, name: 'Trying to hack', supplier: supplier2) - Product.find_by_name('Trying to hack').supplier_id.should == @supplier.id + 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 end end context "with invalid attributes" do it "does not save the new product" do expect{ - post :create, product: {name: ''} + post :create, product: invalid_attributes }.to_not change(Product, :count) end it "re-renders the new method" do - post :create, product: {name: ''} + post :create, product: invalid_attributes response.should render_template :new end end @@ -106,25 +91,21 @@ describe Suppliers::ProductsController do context "valid attributes" do it "located the requested product" do - put :update, id: @product, product: attributes_for(:product, supplier: @supplier) + put :update, id: @product, product: valid_attributes @product.reload assigns(:product).should eq(@product) end it "changes @product's attributes" do - put :update, id: @product, product: attributes_for(:product, name: "ChangedByTest", supplier: @supplier) + put :update, id: @product, product: valid_attributes.merge(name: "ChangedByTest") @product.reload @product.name.should eq("ChangedByTest") end - it "redirects to the updated product" do - put :update, id: @product, product: attributes_for(:product, supplier: @supplier) - response.should redirect_to [:suppliers, :products] - end it "should not be possible to update a product to another supplier" do supplier2 = create :supplier - put :update, id: @product, product: attributes_for(:product, name: "Trying to hack", supplier: supplier2) - Product.find_by_name('Trying to hack').supplier_id.should == @supplier.id + 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 end it "should not be possible to update a product of another supplier" do @@ -137,12 +118,12 @@ describe Suppliers::ProductsController do context "invalid attributes" do it "locates the requested product" do - put :update, id: @product, product: {name: ''} + 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: {name: ''} + put :update, id: @product, product: invalid_attributes response.should render_template :edit end end diff --git a/spec/controllers/suppliers/sections_controller_spec.rb b/spec/controllers/suppliers/sections_controller_spec.rb index c0378ae3..4050452c 100644 --- a/spec/controllers/suppliers/sections_controller_spec.rb +++ b/spec/controllers/suppliers/sections_controller_spec.rb @@ -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 diff --git a/spec/controllers/suppliers/tables_controller_spec.rb b/spec/controllers/suppliers/tables_controller_spec.rb index cebbcd59..888825b3 100644 --- a/spec/controllers/suppliers/tables_controller_spec.rb +++ b/spec/controllers/suppliers/tables_controller_spec.rb @@ -1,10 +1,10 @@ # encoding: UTF-8 require 'spec_helper' -describe Suppliers::TablesController do +describe Suppliers::TablesController, type: :controller do before :each do @supplier = Supplier.find_by_email('supplier@qwaiter.com') || create(:supplier, :confirmed) - controller.stub(:table_params){ controller.params.require(:table).permit! } # allow all parameters since cross parameter injection is tested + #controller.stub(:table_params){ controller.params.require(:table).permit! } # allow all parameters since cross parameter injection is tested sign_in @supplier end @@ -69,19 +69,19 @@ describe Suppliers::TablesController do context "with valid attributes" do it "creates a new table" do expect{ - post :create, table: attributes_for(:table, supplier: @supplier) + post :create, table: {number: 22} }.to change(Table, :count).by(1) end it "redirects to the new table" do - post :create, table: attributes_for(:table, supplier: @supplier) + 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 supplier2 = create :supplier - post :create, table: attributes_for(:table, number: 6, supplier: supplier2) - Table.find_by_number(6).supplier_id.should == @supplier.id + expect { post :create, table: {number: 6, supplier_id: supplier2.id} }.to raise_error + Table.find_by_number(6).should_not be_present end end @@ -106,25 +106,29 @@ describe Suppliers::TablesController do context "valid attributes" do it "located the requested table" do - put :update, id: @table, table: attributes_for(:table, supplier: @supplier) + put :update, id: @table, table: {number: 22} @table.reload assigns(:table).should eq(@table) end - it "changes @table's attributes" do - put :update, id: @table, table: attributes_for(:table, number: "14", supplier: @supplier) + it "changes @table's number attribute" do + put :update, id: @table, table: {number: "14"} @table.reload @table.number.should eq(14) end it "redirects to the updated table" do - put :update, id: @table, table: attributes_for(:table, supplier: @supplier) + 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 - put :update, id: @table, table: attributes_for(:table, number: 6, supplier: supplier2) - Table.find_by_number(6).supplier_id.should == @supplier.id + expect{ + put :update, id: @table.id, table: {number: 6, supplier_id: supplier2.id} + }.to raise_error + @table.reload + @table.supplier_id.should == @supplier.id end it "should not be possible to update a table of another supplier" do diff --git a/spec/support/matchers/broadcast_to_supplier_matcher.rb b/spec/support/matchers/broadcast_to_supplier_matcher.rb index 18b8dbdd..db50bc3d 100644 --- a/spec/support/matchers/broadcast_to_supplier_matcher.rb +++ b/spec/support/matchers/broadcast_to_supplier_matcher.rb @@ -41,6 +41,7 @@ module Matchers def failure_message "supplier #{@supplier_id} did not receive broadcast #{@message} with #{@target_object.inspect} #{@failure_debug_content}" end + def supports_block_expectations?; true; end end def broadcast_to_supplier(*args, &block) diff --git a/spec/support/matchers/broadcast_to_user_matcher.rb b/spec/support/matchers/broadcast_to_user_matcher.rb index 4f5bea7a..976266ca 100644 --- a/spec/support/matchers/broadcast_to_user_matcher.rb +++ b/spec/support/matchers/broadcast_to_user_matcher.rb @@ -41,6 +41,7 @@ module Matchers def failure_message "user #{@user_id} did not receive broadcast #{@message} with #{@target_object.inspect} #{@failure_debug_content}" end + def supports_block_expectations?; true; end end def broadcast_to_user(*args, &block)