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
@@ -80,7 +80,7 @@ module Suppliers
format.json { render json: @product_category, status: :created } format.json { render json: @product_category, status: :created }
else else
format.html { render action: "new" } 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 end
end end
@@ -96,7 +96,7 @@ module Suppliers
format.json { render json: @product_category } format.json { render json: @product_category }
else else
format.html { render action: "edit" } 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 end
end end
@@ -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.week_days.should == [0, 0, 1, 0, 0, 0, 0]
@product_category.full_day.should be true @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_monday ).to be true expect( @product_category.active_on_tuesday ).to be false
expect( @product_category.active_on_wednesday ).to be false expect( @product_category.active_on_wednesday ).to be true
#@product_category.product_ids.should == [@products.first.id] #@product_category.product_ids.should == [@products.first.id]
end end
@@ -63,7 +63,9 @@ end
step "the user order :product_name should be in the order list with price" do |product_name| 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) 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'] quantity = ember_order['quantity']
order_price = quantity * concerning_product.price order_price = quantity * concerning_product.price
within '.product-orders .product-order' do within '.product-orders .product-order' do
@@ -23,10 +23,6 @@ describe Suppliers::ProductCategoriesController, type: :controller do
assigns(:product_categories).should eq([product_category1]) assigns(:product_categories).should eq([product_category1])
end end
it "should render without errors when no objects are present" do
get :index
expect{ render_template :index }.not_to raise_error
end
end end
describe "GET #show" do 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 it "should not be possible to create a product category for another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
expect{ post :create, product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error post :create, product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id)
ProductCategory.find_by_name('Trying to hack').should_not be_present expect( ProductCategory.find_by_name('Trying to hack').supplier_id ).to eq @supplier.id
end end
end end
@@ -76,6 +72,11 @@ describe Suppliers::ProductCategoriesController, type: :controller do
post :create, product_category: invalid_attributes post :create, product_category: invalid_attributes
}.not_to change{ ProductCategory.count } }.not_to change{ ProductCategory.count }
end 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
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 it "should not be possible to update a product category to another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
expect{ put :update, id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
put :update, id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id) @product_category.reload
}.to raise_error expect( @product_category.name ).to eq 'Trying to hack'
ProductCategory.find_by_name('Trying to hack').should_not be_present expect( @product_category.supplier_id ).to eq @supplier.id
end end
it "should not be possible to update a product_category of another supplier" do 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' product_category.name.should == 'Other supplier product_category'
end end
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 end
describe 'DELETE destroy' do describe 'DELETE destroy' do
@@ -22,11 +22,6 @@ describe Suppliers::ProductsController, type: :controller do
get :index get :index
assigns(:products).should eq([product1]) assigns(:products).should eq([product1])
end end
it "should render without errors when no objects are present" do
get :index
expect{ render_template :index }.not_to raise_error
end
end end
describe "GET #show" do 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 it "should not be possible to create a product for another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
expect{ post :create, product: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id) }.to raise_error post :create, product: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id)
Product.find_by_name('Trying to hack').should_not be_present expect( Product.find_by_name('Trying to hack').supplier_id ).to eq @supplier.id
end end
end end
@@ -77,9 +72,9 @@ describe Suppliers::ProductsController, type: :controller do
}.to_not change(Product, :count) }.to_not change(Product, :count)
end end
it "re-renders the new method" do it 'returns an error object' do
post :create, product: invalid_attributes post :create, format: :json, product: invalid_attributes
response.should render_template :new expect( JSON.parse(response.body)['errors'] ).to be_present
end end
end 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 it "should not be possible to update a product to another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
expect{ put :update, id: @product, product: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id) }.to raise_error put :update, id: @product, product: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)
expect( Product.find_by_name 'Trying to hack' ).not_to be_present expect( Product.find_by_name('Trying to hack' ).supplier_id ).to eq @supplier.id
end end
it "should not be possible to update a product of another supplier" do it "should not be possible to update a product of another supplier" do
product = create :product, name: 'Other supplier product' product = create :product, name: 'Other supplier product'
put :update, id: product, product: {name: "Trying to hack"} put :update, id: product, product: {name: "Trying to hack"}
product.reload product.reload
product.name.should == 'Other supplier product' expect( product.name ).to eq 'Other supplier product'
end end
end end
context "invalid attributes" do context "with invalid attributes" do
it "locates the requested product" do it "returns an error response" do
put :update, id: @product, product: invalid_attributes put :update, id: @product, format: :json, product: invalid_attributes
assigns(:product).should eq(@product) expect( JSON.parse( response.body )['errors']).to be_present
end
it "re-renders the edit method" do
put :update, id: @product, product: invalid_attributes
response.should render_template :edit
end end
end end
end end
+9 -1
View File
@@ -1,7 +1,7 @@
module SpecEmberHelpers module SpecEmberHelpers
def ember_store def ember_store
h = page.evaluate_script <<-SCRIPT h = page.evaluate_script <<-SCRIPT
$s = App.__container__.lookup('controller:application').store; $s = App.__container__.lookup('store:main');
JSON.stringify({ JSON.stringify({
lists: $s.all('list').invoke('serialize'), lists: $s.all('list').invoke('serialize'),
orders: $s.all('order').invoke('serialize'), orders: $s.all('order').invoke('serialize'),
@@ -15,6 +15,14 @@ module SpecEmberHelpers
JSON.parse(h) JSON.parse(h)
end 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 def js_path
page.evaluate_script 'location.pathname + location.hash' page.evaluate_script 'location.pathname + location.hash'
end end