Ka Mok
Ka Mok

Reputation: 1988

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

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(foo.email, csv).deliver
  end
end

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?

Upvotes: 0

Views: 1744

Answers (1)

vitomd
vitomd

Reputation: 806

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) { IO.read(Rails.root.join("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
  end
end 

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')

Upvotes: 1

Related Questions