How do you create parent objects for a factory using before(:create) without sparking the parents' after_create callback?

依然范特西╮ 提交于 2019-12-11 18:43:23

问题


I am trying to get this spec to pass:

it "queues up AbsenteeReservationEmailerJob" do
  ActiveJob::Base.queue_adapter = :test
  expect { subject }.to enqueue_job(AbsenteeReservationEmailerJob)
end

My factory looks like so:

FactoryBot.define do
  factory :absentee_reservation do
    amount_cents { 10_000 + rand(1_000_000) }
    creator { create(:user) }

    before(:create) do |absentee_reservation|
      auction = create(:auction_with_one_item_one_bidder)
      item = auction.items.first

      absentee_reservation.auction = auction
      absentee_reservation.bidder = auction.bidders.first
      absentee_reservation.lot = item.current_lot
      absentee_reservation.item = item
    end
  end
end

As you can see, I am expecting that the AbsenteeReservationEmailerJob be enqueued, and it is. However, another parent factory (:auction_with_one_item_one_bidder) is creating a user which is sparking my MailchimpSubscribeUserJob and I am getting the following failure:

Failures:

  1) AbsenteeReservation#absentee_reservation_email queues up AbsenteeReservationEmailerJob
     Failure/Error: expect { subject }.to enqueue_job(AbsenteeReservationEmailerJob)

       expected to enqueue exactly 1 jobs, but enqueued 2
       Queued jobs:
         MailchimpSubscribeUserJob job with [{:user=>#<User id: 496, email: "jewellhackett@west.co", created_at: "2019-01-25 18:01:37", updated_at: "2019-01-25 18:01:37", title: nil, company: nil, salutation: "Miss", first_name: "Randell", last_name: "Rohan", roles: 0, lha_id: nil, notes: nil, tax_exempt: false, tax_id: nil, tax_id_expires_at: nil, email_opt_in: true, rep_id: nil, account_executive_id: nil, rfc_data: nil>}], on queue default
         MailchimpSubscribeUserJob job with [{:user=>#<User id: 497, email: "aureagerhold@bahringerschaden.org", created_at: "2019-01-25 18:01:37", updated_at: "2019-01-25 18:01:37", title: nil, company: nil, salutation: "Miss", first_name: "Erica", last_name: "Fadel", roles: 0, lha_id: nil, notes: nil, tax_exempt: false, tax_id: nil, tax_id_expires_at: nil, email_opt_in: true, rep_id: nil, account_executive_id: nil, rfc_data: nil>}], on queue default

Can/should I expect only one type of job be enqueued, the AbsenteeReservationEmailerJob? If so, then please feel free to see my other question: Can you expect only one type of job to be enqueued in Rspec?

PS: I've also looked at Skip callbacks on Factory Girl and Rspec and User.skip_callback(:create) seems to be ignored if I put it in a before block before the spec, or in the before block in the :absentee_reservation factory.

Thanks!

来源:https://stackoverflow.com/questions/54370562/how-do-you-create-parent-objects-for-a-factory-using-beforecreate-without-spa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!