mardi 26 janvier 2016

How to test that an ActiveJob calls a service object

I have a PlantTree job that calls a PlanTree service object. I would like to test the job to ascertain that it instantiates the service with a tree argument and calls the call method.

I'm not interested in what the service does. It has its own test, and I don't want to repeat those tests for the job.

# app/jobs/plant_tree_job.rb
class PlantTreeJob < ActiveJob::Base
  def perform(tree)
    PlantTree.new(tree).call
  end
end

# app/services/plant_tree.rb
class PlantTree
  def initialize(tree)
    @tree = tree
  end

  def call
    # Do stuff that plants the tree
  end
end

I'm using Rails' default stack, which uses MiniTest. Here's how I'm trying:

class PlantTreeJobTest < ActiveJob::TestCase
  setup do
    @tree = create(:tree)
  end

  test "calls the PlantTree service" do
    service = PlantTree.new(@tree)

    service.stub(:call, service) do
      # how to assert that `call` was called?
    end
  end
end

I think I'm doing this wrong. I don't know if it is necessary to create the tree and instantiate the service for what I'm trying to achieve.

Aucun commentaire:

Enregistrer un commentaire