私のテストは次のようになります。
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
期間オブジェクトとイベントオブジェクトを作成する必要がありますか?もしそうなら、どのように?いろいろな工場で「ピリオド」、「プロダクト」、「イベント」の順で自動的に作成されると思いますので、これは問題ではないと思います。
ここからどこを見ればいいのか、何かアイデアはありますか?