我的测试如下所示:
def setup
@period_registration= FactoryGirl.create(:period_registration)
end
test "should post save_period" do
sign_in(FactoryGirl.create(:user))
assert_difference('PeriodRegistration.count') do
post :save_period, period_registration: FactoryGirl.attributes_for(:period_registration)
end
assert_not_nil assigns(:period_registration)
end
但是当我运行它时,我得到了这个错误:
1) Error:
test_should_post_save_period(PeriodRegistrationsControllerTest):
NoMethodError: undefined method `event' for nil:NilClass
这是我的控制器:
def save_period
@period_registration = PeriodRegistration.new(params[:registration])
@period_registration.save
flash[:success] = "Successfully Registered for Session."
redirect_to event_url(@period_registration.period.event)
end
我的工厂是这样的:
factory :event do
name 'First Event'
street '123 street'
city 'Chicago'
state 'Iowa'
date Date.today
end
factory :period do
name 'First Period'
description 'This is a description'
start_time Time.now + 10.days
end_time Time.now + 10.days + 2.hours
event
product
end
factory :period_registration do
user
period
end
我需要创建一个周期对象和一个事件对象吗?如果有怎么办?我认为这不是问题,因为我相信在各个工厂中通过“周期”、“产品”和“事件”自动创建这些。
关于从这里看哪里的任何想法?