Rails/Rspec: Testing delayed_job mails

前端 未结 5 819
你的背包
你的背包 2021-02-01 17:37

Just wondering how to test that actionmailer requests are actually sent to the delayed_job que in rspec.

I would have assumed it was quite simple, but my delayed_job que

5条回答
  •  情话喂你
    2021-02-01 17:55

    This thread is a bit old, but here is my go at it:

    Create a function expect_jobs

    def expect_jobs n, time = nil
      expect(Delayed::Job.count).to eq(n)
      Timecop.travel(time) unless time.nil?
      successes, failures = Delayed::Worker.new.work_off
      expect(successes).to eq(n)
      expect(failures).to eq(0)
      expect(Delayed::Job.count).to eq(0)
      Timecop.travel(Time.now) unless time.nil?
    end
    

    Then simply call it before checking if the callback has done its job. eg:

    it "sends a chapter to the admin user" do
      post :chapter_to_user, { chapter: @book.chapters.first}
      expect_jobs(1)
      SubscribeMailer.should have(1).delivery
      SubscribeMailer.deliveries.should have(1).attachment
    end
    

    This seems to work on my side, and allows me to run both my delayed jobs and my methods.

提交回复
热议问题