私のテストは失敗し、テストレポートに記載されています
失敗/エラー: assigns(:items).should eq(item) expected: [# Item id: 756, product_name: "Item 1", product_price: 310.25, product_balance: 3, active: 1, created_at: "2012-10- 11 20:10:23"、updated_at: "2012-10-11 20:10:23"] 取得: []
テストが失敗する理由を教えてください。
cart_controller.rb ファイル
class CartController < ApplicationController
def index
@cart = session[:cart] || {}
@items = Item.where( id: @cart.keys )
@cart_cost = 0
@items.each do | item |
@cart_cost += @cart[ item.id.to_s ]*item.product_price
end
end
end
cart_controller_spec.rb
describe CartController do
context "when user is logged in" do
before (:each) do
v = Visitor.create! :username => 'John Doe', :password => '123456'
session[:logged_in] = 'logged_in'
session[:cart] = {}
@cart = session[:cart]
@normal_item = Item.create! :product_name => 'Item 1',
:product_price => '310.25', :product_balance => '3', :active => '1'
end
describe "GET 'index'" do
it "assigns session cart variable as @cart" do
get :index
@cart[ @normal_item.id ] = 1
assigns(:cart).should eq(session[:cart])
item = Item.where( id: @cart.keys )
assigns(:items).should eq(item)
end
end
end
end