Commit 973f4051 by vulehuan

spec: card - remove product with invalid information

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