我有一个看起来像这样的测试:
test "should get create" do
current_user = FactoryGirl.build(:user, email: 'not_saved_email@example.com')
assert_difference('Inquiry.count') do
post :create, FactoryGirl.build(:inquiry)
end
assert_not_nil assigns(:inquiry)
assert_response :redirect
end
这是测试控制器的这一部分:
def create
@inquiry = Inquiry.new(params[:inquiry])
@inquiry.user_id = current_user.id
if @inquiry.save
flash[:success] = "Inquiry Saved"
redirect_to root_path
else
render 'new'
end
end
和工厂:
FactoryGirl.define do
factory :inquiry do
product_id 2
description 'I have a question about....'
end
end
但我在测试中不断出错:
1) Error:
test_should_get_create(InquiriesControllerTest):
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
我究竟做错了什么?我需要设置 current_user,我相信我正在测试中,但显然,这不起作用。