From 3941529d0d5212b1277764cf848015a6a51128e5 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Thu, 4 Dec 2014 19:00:53 +0100 Subject: [PATCH] More spec fixing --- .../product_categories_controller.rb | 4 +-- .../suppliers/product_category_steps.rb | 4 +-- .../users/order_products_steps.rb | 4 ++- .../product_categories_controller_spec.rb | 28 +++++++++------ .../suppliers/products_controller_spec.rb | 34 +++++++------------ spec/support/ember_helpers.rb | 10 +++++- 6 files changed, 46 insertions(+), 38 deletions(-) diff --git a/app/controllers/suppliers/product_categories_controller.rb b/app/controllers/suppliers/product_categories_controller.rb index 04dd9d4e..7bab4af5 100644 --- a/app/controllers/suppliers/product_categories_controller.rb +++ b/app/controllers/suppliers/product_categories_controller.rb @@ -80,7 +80,7 @@ module Suppliers format.json { render json: @product_category, status: :created } else format.html { render action: "new" } - format.json { render json: @product_category.errors, status: :unprocessable_entity } + format.json { render json: {errors: @product_category.errors}, status: :unprocessable_entity } end end end @@ -96,7 +96,7 @@ module Suppliers format.json { render json: @product_category } else format.html { render action: "edit" } - format.json { render json: @product_category.errors, status: :unprocessable_entity } + format.json { render json: {errors: @product_category.errors}, status: :unprocessable_entity } end end end diff --git a/spec/acceptance_steps/suppliers/product_category_steps.rb b/spec/acceptance_steps/suppliers/product_category_steps.rb index a9bc646f..785fca62 100644 --- a/spec/acceptance_steps/suppliers/product_category_steps.rb +++ b/spec/acceptance_steps/suppliers/product_category_steps.rb @@ -21,8 +21,8 @@ step "then new product category with proper properties should have been created" #@product_category.week_days.should == [0, 0, 1, 0, 0, 0, 0] @product_category.full_day.should be true expect( @product_category.active_on_monday ).to be true - expect( @product_category.active_on_monday ).to be true - expect( @product_category.active_on_wednesday ).to be false + expect( @product_category.active_on_tuesday ).to be false + expect( @product_category.active_on_wednesday ).to be true #@product_category.product_ids.should == [@products.first.id] end diff --git a/spec/acceptance_steps/users/order_products_steps.rb b/spec/acceptance_steps/users/order_products_steps.rb index ff935202..972983d9 100644 --- a/spec/acceptance_steps/users/order_products_steps.rb +++ b/spec/acceptance_steps/users/order_products_steps.rb @@ -63,7 +63,9 @@ end step "the user order :product_name should be in the order list with price" do |product_name| concerning_product = Product.find_by_name(product_name) - ember_order = ember_store['product_orders'].find{|po| po['product_id'] == concerning_product.id} + #ember_order = ember_store['product_orders'].find{|po| po['product_id'] == concerning_product.id} + ember_order = ember_find('product_order', concerning_product.id) + binding.pry quantity = ember_order['quantity'] order_price = quantity * concerning_product.price within '.product-orders .product-order' do diff --git a/spec/controllers/suppliers/product_categories_controller_spec.rb b/spec/controllers/suppliers/product_categories_controller_spec.rb index 6d5b4370..ade74245 100644 --- a/spec/controllers/suppliers/product_categories_controller_spec.rb +++ b/spec/controllers/suppliers/product_categories_controller_spec.rb @@ -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 diff --git a/spec/controllers/suppliers/products_controller_spec.rb b/spec/controllers/suppliers/products_controller_spec.rb index 09fbed40..2bdac743 100644 --- a/spec/controllers/suppliers/products_controller_spec.rb +++ b/spec/controllers/suppliers/products_controller_spec.rb @@ -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 diff --git a/spec/support/ember_helpers.rb b/spec/support/ember_helpers.rb index bbef7a2f..c0acf9a6 100644 --- a/spec/support/ember_helpers.rb +++ b/spec/support/ember_helpers.rb @@ -1,7 +1,7 @@ module SpecEmberHelpers def ember_store h = page.evaluate_script <<-SCRIPT - $s = App.__container__.lookup('controller:application').store; + $s = App.__container__.lookup('store:main'); JSON.stringify({ lists: $s.all('list').invoke('serialize'), orders: $s.all('order').invoke('serialize'), @@ -15,6 +15,14 @@ module SpecEmberHelpers JSON.parse(h) end + def ember_find(typeKey, id) + h = page.evaluate_script <<-SCRIPT + $s = App.__container__.lookup('store:main'); + record = $s.all('#{typeKey}').findBy('id', '#{id}'); + record ? record.serialize() : null + SCRIPT + end + def js_path page.evaluate_script 'location.pathname + location.hash' end