Question: How can I test an Active Job method with mocks in RSpec?


How can I test an Active Job method with mocks in RSpec?

Answers 1
Added at 2016-10-03 20:10

Given this Active Job method that has a lot of overhead in one of the methods it calls. In this case, it's the foo.generate that makes a big CSV file.

class SomeJob < ApplicationJob
  queue_as :default

  def perform(foo)
    csv = foo.generate
    JobMailer.send_csv(, csv).deliver

I know that you can use mocks to create the output for foo.generate, but the more I do it, I feel like I'm just writing out the entire method in my test. Can someone give me some guidance?

nr: #1 dodano: 2016-10-03 21:10

You could have a csv dummy file, and read the content from there, and then mock the response of foo.generate. Not sure what are you trying to test, so I left the except pending.

describe "csv" do
  let(:csv_doc) {"spec", "fixtures", "dummy_csv.csv")) }

  it "test something" do
    allow_any_instance_of(Foo).to receive(:generate).and_return(csv_doc)
    expect(something).to be true

That´s if you care about the csv content, if not you just need to do something like this. In this case Foo is the foo type.

allow_any_instance_of(Foo).to receive(:generate).and_return('something')
Source Show
◀ Wstecz