我有一个测试会产生以下错误:
1) Failure:
test_should_get_create(ProductRequestsControllerTest) [/Users/noahc/Dropbox/mavens/test/functional/product_requests_controller_test.rb:37]:
"ProductRequest.count" didn't change by 1.
<2> expected but was
<1>.
如何解决这个问题呢?具体来说,我怎样才能得到更具体的详细错误?
这是我的测试:
test "should get create" do
sign_in(FactoryGirl.create(:user))
assert_difference('ProductRequest.count') do
post :create, product_request: FactoryGirl.attributes_for(:product_request)
end
assert_response :success
end
这是我的控制器:
def create
cart = current_cart
rows = CartRow.find_all_by_cart_id(cart.id)
rows.each do |row|
product_request = ProductRequest.new(params[:product_request])
product_request.user_id = current_user.id
product_request.product_id = row.product_id
product_request.quantity = row.quantity
product_request.save
end
redirect_to root_path
end
我认为问题在于我没有定义购物车。如何创建unit :: test可以看到的购物车?我尝试过使用FactoryGirl创建一个购物车,但这似乎不起作用。
carts_factory.rb
FactoryGirl.define do
factory :cart do
end
end
更新了测试:
test "should get create" do
sign_in(FactoryGirl.create(:user))
user = FactoryGirl.create(:user)
product = FactoryGirl.create(:product)
assert_difference('ProductRequest.count') do
post :create, product_request: FactoryGirl.attributes_for(:product_request, user: user.id, product: product.id)
end
assert_response :success
end
和current_cart
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
第二次更新
我按照你的建议更新了工厂。
以下是我的测试现在的样子:
test "should get create" do
user = FactoryGirl.create(:user)
cart = FactoryGirl.create(:cart_with_1_row)
product = FactoryGirl.create(:product)
sign_in(user)
product = FactoryGirl.create(:product)
assert_difference('ProductRequest.count') do
post :create, { product_request: FactoryGirl.attributes_for(:product_request, user_id: user.id, product_id: product.id, cart_id: cart.id) }
end
assert_response :success
end
这是在测试控制台中:
irb(main):016:0> a = { product_request: FactoryGirl.attributes_for(:product_request, user_id: user.id, product_id: product.id, cart_id: cart.id) }
=> {:product_request=>{:quantity=>10, :street=>"123 street", :city=>"Some City", :state=>"Iowa", :zip=>"13829", :user_id=>1, :product_id=>2, :cart_id=>1}}
答案 0 :(得分:1)
首先,CartRow.find_all_by_cart_id(cart.id)
,这不是好设计。当您向购物车模型询问其行时,情况要好得多,例如:rows = cart.rows
我认为问题在于你的购物车内没有行。
我看到你在购物车中存储购物车ID,但是当你在测试中调用控制器时,你不会提供会话。您需要创建购物车和购物车的行,并在调用控制器之前将cart_id存储在会话中。 将当前会话和会话与cart_id 合并非常重要。例如:
test "should get create" do
user = FactoryGirl.create(:user)
cart = FactoryGirl.create(:cart_with_1_row)
sign_in(user)
product = FactoryGirl.create(:product)
assert_difference('ProductRequest.count') do
post :create, { product_request: FactoryGirl.attributes_for(:product_request, user: user.id, product: product.id) }, { cart_id: cart.id }.merge(session)
end
assert_response :success
end
您还需要更新购物车和购物车的行工厂:
FactoryGirl.define do
factory :cart do
factory :cart_with_1_row do
after(:create) do |cart|
FactoryGirl.create(:cart_row, cart: cart)
end
end
end
factory :cart_row do
cart
end
end
我认为您的CartRow模型如下:
class CartRow < ActiveRecord::Base
belongs_to :cart
end
答案 1 :(得分:0)
问题肯定来自购物车。
如果您不想处理在FactoryGirl中创建购物车(我建议),您可以在测试中删除current_card,这也可以做同样的事情。
然而,模仿比在FactoryGirl中创建购物车要复杂得多,如果你计划在未来使用它,那么FactoryGirl绝对是你的选择。