Commit 973f4051 by vulehuan

spec: card - remove product with invalid information

parent a823ed3d
...@@ -12,7 +12,7 @@ class CardsController < ApplicationController ...@@ -12,7 +12,7 @@ class CardsController < ApplicationController
if !params[:card_action].nil? && params[:card_action] == 'remove' if !params[:card_action].nil? && params[:card_action] == 'remove'
# Nothing to delete # Nothing to delete
if @card_infos.empty? if @card_infos.empty?
redirect_to cards_path redirect_to cards_path and return
else else
card_items = @card_infos[:card_items] card_items = @card_infos[:card_items]
found = false found = false
...@@ -25,20 +25,20 @@ class CardsController < ApplicationController ...@@ -25,20 +25,20 @@ class CardsController < ApplicationController
found = true found = true
end end
flash[:error] = "Invalid request (product not exist in card)" if !found flash[:error] = "Invalid request (product not exist in card)" if !found
redirect_to cards_path redirect_to cards_path and return
end end
end end
elsif !params[:card_action].nil? && params[:card_action] == 'update' elsif !params[:card_action].nil? && params[:card_action] == 'update'
# Nothing to update # Nothing to update
if @card_infos.empty? if @card_infos.empty?
flash[:error] = "Invalid request" flash[:error] = "Invalid request"
redirect_to cards_path redirect_to cards_path and return
else else
quantity = params[:quantity] quantity = params[:quantity]
# Invalid request # Invalid request
if quantity.nil? || quantity.to_i <= 0 if quantity.nil? || quantity.to_i <= 0
flash[:error] = "Invalid quantity" flash[:error] = "Invalid quantity"
redirect_to cards_path redirect_to cards_path and return
else else
card_items = @card_infos[:card_items] card_items = @card_infos[:card_items]
card_items.each_with_index do |card_item, key| card_items.each_with_index do |card_item, key|
...@@ -50,7 +50,7 @@ class CardsController < ApplicationController ...@@ -50,7 +50,7 @@ class CardsController < ApplicationController
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
end end
end end
redirect_to cards_path redirect_to cards_path and return
end end
end end
else else
...@@ -63,7 +63,7 @@ class CardsController < ApplicationController ...@@ -63,7 +63,7 @@ class CardsController < ApplicationController
customer_info = Hash.new customer_info = Hash.new
@card_infos = { card_items: card_items, customer_info: customer_info } @card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path redirect_to cards_path and return
else else
card_items = @card_infos[:card_items] card_items = @card_infos[:card_items]
found = false found = false
...@@ -81,12 +81,12 @@ class CardsController < ApplicationController ...@@ -81,12 +81,12 @@ class CardsController < ApplicationController
@card_infos = { card_items: card_items, customer_info: customer_info } @card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
end end
redirect_to cards_path redirect_to cards_path and return
end end
end end
else else
flash[:error] = "Invalid product" flash[:error] = "Invalid product"
redirect_to products_path redirect_to products_path and return
end end
end end
end end
......
...@@ -4,21 +4,28 @@ describe "CardPages" do ...@@ -4,21 +4,28 @@ describe "CardPages" do
subject { page } subject { page }
describe "step 1" do describe "step 1" do
before(:each) do before(:all) do
@product_category = FactoryGirl.create(:product_category) @product_category = FactoryGirl.create(:product_category)
@user = FactoryGirl.create(:user) @user = FactoryGirl.create(:user)
end
before(:each) do
5.times { FactoryGirl.create(:product, product_category_id: @product_category.id, user_id: @user.id) } 5.times { FactoryGirl.create(:product, product_category_id: @product_category.id, user_id: @user.id) }
Product.all.each do |product| Product.all.each do |product|
visit product_path(product) visit product_path(product)
page.find('.product-detail').click_link('Order') page.find('.product-detail').click_link('Order')
end end
end end
after(:each) do
Product.delete_all after(:all) do
User.delete_all User.delete_all
ProductCategory.delete_all ProductCategory.delete_all
end end
after(:each) do
Product.delete_all
end
describe "when update quantity" do describe "when update quantity" do
describe "with invalid information" do describe "with invalid information" do
describe "when product not exist" do describe "when product not exist" do
...@@ -85,35 +92,24 @@ describe "CardPages" do ...@@ -85,35 +92,24 @@ describe "CardPages" do
describe "when remove a product" do describe "when remove a product" do
describe "with invalid information" do describe "with invalid information" do
# let(:product) { Product.first }
# before { visit cards_path(product_id: product.id) }
describe "when product not exist" do describe "when product not exist" do
# before do before do
# visit cards_path(card_action: 'update', product_id: 0, quantity: "abc") visit cards_path(card_action: 'update', product_id: 0)
# end
# it { should have_selector('.alert.alert-error', text: 'Invalid') }
end end
# it "when product exist in db, but not exist in card" do it { should have_selector('.alert.alert-error', text: 'Invalid') }
# product = Product.first end
# product_not_in_card = Product.last
# visit cards_path(product_id: product.id)
# visit cards_path(card_action: 'remove', product_id: product_not_in_card.id)
# page.should have_selector('.alert.alert-error', text: 'Invalid') describe "when product exist in db, but not exist in card" do
# end let(:product_not_in_card) { FactoryGirl.create(:product, product_category_id: @product_category.id, user_id: @user.id) }
# describe "when product exist in db, but not exist in card" do
# let(:product_not_in_card) { Product.last }
# before do before do
# visit cards_path(card_action: 'remove', product_id: product_not_in_card.id) visit cards_path(card_action: 'remove', product_id: product_not_in_card.id)
# end end
# it { should have_selector('.alert.alert-error', text: 'Invalid') } it { should have_selector('.alert.alert-error', text: 'Invalid') }
# end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment