ohhh
ohhh

Reputation: 707

How do I spy on objects within the context of a rake task with rspec?

I have the following setup:

class Simple
  def self.test; end
end

task a_simple_job: :environment do
  counter = 0
  while counter < 2    
    Simple.test
    counter += 1
  end
end

require "rails_helper"
describe "a_simple_job" do
  subject { Rake::Task[:a_simple_job].invoke }
  before do
    Rails.application.load_tasks
    allow(Simple).to receive(:test)
    subject
  end

  # It seems like rspec cannot spy on objects created in the context of rake tasks
  it "calls test twice" do
    expect(Simple).to receive(:test).once
  end
end

Why am I getting:

(Simple (class)).test

expected: 1 time with any arguments

received: 0 times with any arguments

Is there something I'm missing here about rake task context and rspec?

Upvotes: 0

Views: 108

Answers (1)

tonystrawberry
tonystrawberry

Reputation: 241

You should have used have_received instead of receive. That's because you already called your task (and consequently Simple.test) before the assertion.

Reference: https://rubydoc.info/github/rspec/rspec-mocks/RSpec%2FMocks%2FExampleMethods:have_received

I made it work on my machine.

# lib/tasks/simple_task.rake

task a_simple_job: :environment do
  counter = 0
  while counter < 2
    Simple.test
    puts "counter: #{counter}"
    counter += 1
  end
end
# lib/simple.rb

class Simple
  def self.test
    puts "test"
  end
end
# spec/lib/tasks/simple_task_spec.rb

require "rails_helper"
require "simple"

describe "a_simple_job" do
  subject { Rake::Task[:a_simple_job].invoke }
  before do
    Rails.application.load_tasks
    allow(Simple).to receive(:test)
  end

  it "calls test twice" do
    Rake::Task[:a_simple_job].invoke
    expect(Simple).to have_received(:test).twice # Because it is called twice in the loop
  end
end

Here is the test result.

test result

Upvotes: 0

Related Questions