我正在使用我的应用程序构建购物车,并且我的Flash消息未更新。有人可以引导我朝正确的方向前进吗?
测试:
it "the message correctly increments for multiple items" do
user = User.create!(name: "Test Person", address: "123 lane", city: "Denver", state: "CO", zip: "80205", email: "test@test.com", password: "12345", password_confirmation: "12345")
item = Item.create!(name:"Item_1", thumbnail:"whatever", price: 1, inventory: 1, description:"This is item 1", enabled: true, user_id: user.id)
visit items_path
click_button "Add Item"
expect(page).to have_content("You now have 1 copy of #{item.name} in your cart.")
click_button "Add Item"
expect(page).to have_content("You now have 2 copies of #{item.name} in your cart.")
结束
购物车控制器:
class CartsController < ApplicationController
include ActionView::Helpers::TextHelper
def create
@item = Item.find(params[:item_id])
session[:cart] ||= Hash.new(0)
session[:cart][@item.id] ||= 0
session[:cart][@item.id] = session[:cart][@item.id] += 1
quantity = session[:cart][@item.id]
flash[:notice] = "You now have #{pluralize(quantity, "copy")} of #{@item.name} in your cart."
redirect_to items_path
end
结束
我得到的错误:
1) When a user adds songs to their cart the message correctly increments
for multiple songs
Failure/Error: expect(page).to have_content("You now have 2 copies of
#{item.name} in your cart.")
expected to find text "You now have 2 copies of Item_1 in your cart.
" in "You now have 1 copy of Item_1 in your cart.\nRegister or Log In\nYou
now have 1 copy of Item_1 in your cart.\nItem_1\nwhatever"
# ./spec/features/user_can_add_items_to_their_cart_spec.rb:29:in `bloc
k (2 levels) in <top (required)>'
Finished in 1.32 seconds (files took 2.07 seconds to load)
45 examples, 1 failure