代码之家  ›  专栏  ›  技术社区  ›  Antarr Byrd

更改名称后工厂失败

  •  0
  • Antarr Byrd  · 技术社区  · 7 年前

    :notification_event 但是当我把名字改成 player_notification_event 它因错误而失败

    还有,我的另一家工厂 :property_notification_event 也会失败

    未初始化的常量属性NotificationEvent。

      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 回复  |  直到 7 年前
        1
  •  1
  •   Marcin Kołodziej    7 年前

    违约 factory_bot factory ,如果你没有通过考试 class 明确地(检查 official guide

    factory :player_notification_event, class: NotificationEvent do ...
    
        2
  •  2
  •   max Mike Williams    7 年前

    你可以在这里复制整个工厂。

    […]为每个类定义一个基本工厂是一个很好的实践,它只包含 创建它所需的属性。然后,创建更具体的 从该基本父级继承的工厂。工厂定义是 仍然是代码,所以保持干燥。
    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 您不需要手动指定它。