mardi 12 avril 2016

Testing nested Sidekiq jobs with rspec

Lets say I have the following base Ruby class:

class BaseWorker
  include Sidekiq::Worker
end

and the following two queues and one helper class:

class Finder
  class << self
    def find(id)
      AwesomeDBClass.find(id)
    end
  end
end

class Queue1 extend BaseWorker
  def perform(variable)
    if variable.some_attribute
      ...
    end
  end
end

class Queue2 extend BaseWorker
  def perform id
    local_variable = Finder.find(id)
    Queue1.perform(local_variable)
  end
end

And the following test harness:

let(:local_variable) do
   double(:local_var, some_attribute: true)
end

let(:local_variable2) do
  OpenStruct(some_attribute: true)
end

before do
  allow(Finder).to_receive(:find).and_return(:local_variable)
  ... code that triggers queue2 ...
  queue2.drain
  queue1.drain
end

This fails if local_variable (the double) is returned with:

  undefined method `some_attribute' for {"__expired"=>false, "name"=>"local_variable"}:Hash

And if local_variable2 (the openstruct) is returned with:

 undefined method `some_attribute' for {"table"=>{"some_attribute"=>true}}:Hash

interestingly, if inspecting either in queue2 (with a puts say) their values are correct.

Also, when reading the code for the two libraries:

http://ift.tt/1qlLkfH

http://ift.tt/1qN9n8f

It seems that the state suggest that the constructor has run, but some other thing (method_missing binding, eg other method definitions on the class and/or assign_stubs running) has not happened. Any ideas as to how or why this is happening would be appreciated.

Aucun commentaire:

Enregistrer un commentaire