工厂改名后倒闭
Factory fails after changing name
我有一个工厂,当它的符号是 :notification_event
时工作正常,但是当我将名称更改为 player_notification_event
时它失败并显示错误
uninitialized constant PlayerNotificationEvent.
此外,我的另一个工厂 :property_notification_event
也出现故障,出现错误
uninitialized constant PropertyNotificationEvent.
失败的工厂
factory :player_notification_event do
notification_eventable_type 'Player'
association :notification_eventable, factory: :player
unread_count 1
last_notif_unread_count 0
last_email_message_count 0
last_email_time 5.hours.ago
last_notif_time 3.hours.ago
end
factory :property_notification_event do
notification_eventable_type 'Property'
association :notification_eventable, factory: :property
unread_count 1
last_notif_unread_count 0
last_email_message_count 0
last_email_time 5.hours.ago
last_notif_time 3.hours.ago
end
不合格规格
let(:player_notification_event) { create :player_notification_event }
let(:property_notification_event) { create :property_notification_event }
it 'sends email to player' do
player = player_notification_event.notification_eventable
allow(UnreadMessagesMailer).to receive_message_chain(:player_email, :deliver_now!)
described_class.perform
expect(UnreadMessagesMailer).to have_received(:player_email)
end
it 'sends email to property' do
property = property_notification_event.notification_eventable
allow(UnreadMessagesMailer).to receive_message_chain(:property_email, :deliver_now!)
described_class.perform
expect(UnreadMessagesMailer).to have_received(:property_email)
end
通过规范
let(:player_notification_event) { create :notification_event }
it 'sends email to player' do
player = player_notification_event.notification_eventable
allow(UnreadMessagesMailer).to receive_message_chain(:player_email, :deliver_now!)
described_class.perform
expect(UnreadMessagesMailer).to have_received(:player_email)
end
路过工厂
factory :notification_event do
notification_eventable_type 'Player'
association :notification_eventable, factory: :player
unread_count 1
last_notif_unread_count 0
last_email_message_count 0
last_email_time 5.hours.ago
last_notif_time 3.hours.ago
end
factory_bot
的默认设置是查找与 factory
中的第一个参数同名的 class,如果你没有显式传递 class
(检查 official guide)。试试这个:
factory :player_notification_event, class: NotificationEvent do ...
您可以在这里使用继承而不是复制整个工厂。
[...] it's good practice to define a basic factory for each class with only
the attributes required to create it. Then, create more specific
factories that inherit from this basic parent. Factory definitions are
still code, so keep them DRY.
https://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md
factory :notification_event do
unread_count 1
last_notif_unread_count 0
last_email_message_count 0
last_email_time 5.hours.ago
last_notif_time 3.hours.ago
factory :player_notification_event do
notification_eventable_type 'Player'
association :notification_eventable, factory: :player
end
factory :property_notification_event do
notification_eventable_type 'Property'
association :notification_eventable, factory: :property
end
end
由于模型 class 派生自父模型 factory :notification_event
,因此您无需手动指定它。
我有一个工厂,当它的符号是 :notification_event
时工作正常,但是当我将名称更改为 player_notification_event
时它失败并显示错误
uninitialized constant PlayerNotificationEvent.
此外,我的另一个工厂 :property_notification_event
也出现故障,出现错误
uninitialized constant PropertyNotificationEvent.
失败的工厂
factory :player_notification_event do
notification_eventable_type 'Player'
association :notification_eventable, factory: :player
unread_count 1
last_notif_unread_count 0
last_email_message_count 0
last_email_time 5.hours.ago
last_notif_time 3.hours.ago
end
factory :property_notification_event do
notification_eventable_type 'Property'
association :notification_eventable, factory: :property
unread_count 1
last_notif_unread_count 0
last_email_message_count 0
last_email_time 5.hours.ago
last_notif_time 3.hours.ago
end
不合格规格
let(:player_notification_event) { create :player_notification_event }
let(:property_notification_event) { create :property_notification_event }
it 'sends email to player' do
player = player_notification_event.notification_eventable
allow(UnreadMessagesMailer).to receive_message_chain(:player_email, :deliver_now!)
described_class.perform
expect(UnreadMessagesMailer).to have_received(:player_email)
end
it 'sends email to property' do
property = property_notification_event.notification_eventable
allow(UnreadMessagesMailer).to receive_message_chain(:property_email, :deliver_now!)
described_class.perform
expect(UnreadMessagesMailer).to have_received(:property_email)
end
通过规范
let(:player_notification_event) { create :notification_event }
it 'sends email to player' do
player = player_notification_event.notification_eventable
allow(UnreadMessagesMailer).to receive_message_chain(:player_email, :deliver_now!)
described_class.perform
expect(UnreadMessagesMailer).to have_received(:player_email)
end
路过工厂
factory :notification_event do
notification_eventable_type 'Player'
association :notification_eventable, factory: :player
unread_count 1
last_notif_unread_count 0
last_email_message_count 0
last_email_time 5.hours.ago
last_notif_time 3.hours.ago
end
factory_bot
的默认设置是查找与 factory
中的第一个参数同名的 class,如果你没有显式传递 class
(检查 official guide)。试试这个:
factory :player_notification_event, class: NotificationEvent do ...
您可以在这里使用继承而不是复制整个工厂。
[...] it's good practice to define a basic factory for each class with only the attributes required to create it. Then, create more specific factories that inherit from this basic parent. Factory definitions are still code, so keep them DRY.
https://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md
factory :notification_event do
unread_count 1
last_notif_unread_count 0
last_email_message_count 0
last_email_time 5.hours.ago
last_notif_time 3.hours.ago
factory :player_notification_event do
notification_eventable_type 'Player'
association :notification_eventable, factory: :player
end
factory :property_notification_event do
notification_eventable_type 'Property'
association :notification_eventable, factory: :property
end
end
由于模型 class 派生自父模型 factory :notification_event
,因此您无需手动指定它。