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

Question

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

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

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?

Answers
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) { 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')
Source Show
◀ Wstecz