Завод не работает после смены имени - PullRequest
0 голосов
/ 08 ноября 2018

У меня есть фабрика, которая работает нормально, когда ее символом является :notification_event, но когда я изменяю имя на player_notification_event, происходит сбой с ошибкой

неинициализированная константа PlayerNotificationEvent.

Кроме того, моя другая фабрика :property_notification_event также не работает, с ошибкой

неинициализированная константа 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

Ответы [ 2 ]

0 голосов
/ 08 ноября 2018

Вы можете использовать наследование здесь вместо дублирования всей фабрики.

[...] рекомендуется определять базовую фабрику для каждого класса, используя только атрибуты, необходимые для его создания. Затем создайте более конкретный фабрики, которые наследуют от этого основного родителя. Фабричные определения код, так что держите их СУХОЙ.
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

Поскольку класс модели получен из родительского элемента factory :notification_event, вам не нужно указывать его вручную.

0 голосов
/ 08 ноября 2018

По умолчанию factory_bot - поиск класса с тем же именем, что и у первого аргумента в factory, если вы не передаете class явно (проверьте официальное руководство ). Попробуйте это:

factory :player_notification_event, class: NotificationEvent do ...
...