Search code examples
rspecsucker-punch

Using RSpec to test SuckerPunch::Job


I've defined the following SuckerPunch Job:

class MyWorker
  include SuckerPunch::Job

  def perform(account)
    @account = account
  end

  def params
    @account
  end
end

And I want to test it using RSpec:

describe MyWorker do
  before { subject.perform("[email protected]") }

  its(:params) { should eq "[email protected]" }
end

This works fine when testing without include SuckerPunch::Job. Probably because the subject refers to an instance of ActorProxy instead of MyWorker.

How should I test MyWorker? Or how should I get access to the instance of MyWorker? I've read the Gotchas described in the Celluloid wiki, but the #wrapped_object method doesn't to exist (anymore).


Solution

  • If you look at https://github.com/brandonhilkert/sucker_punch/blob/master/lib/sucker_punch/job.rb, you'll see that MyWorker.new is indeed getting redefined to return something other than an instance of the MyWorker class. It's actually a Celluloid::PoolManager.

    Anyway, if you want to access the params instance method defined within MyWorker, you can allocate an instance of the class, as follows:

    describe MyWorker do
      subject { MyWorker.allocate }
      before { subject.perform("[email protected]") }
    
      its(:params) { should eq "[email protected]" }
    end
    

    allocate is an instance method of Class is like new, but without the call to initialize.