Question

I'm working on a custom job class for Delayed::Job so that I can observe the jobs as they are being run and moving through their various lifecycle events. In this particular case, I'm interested in when they complete so that I can maintain an explicit ordering of the jobs that are run.

The production code:

class ObservableJob
  def self.new_with_listener(listener)
    job = ObservableJob.new
    job.add_listener(listener)
    job
  end

  def success
    notify_listeners(:on_success)
  end

  def add_listener(listener)
    (@listeners ||= []) << listener
  end

  def notify_listeners(event_name, *args)
    @listeners && @listeners.each do |listener|
      if listener.respond_to?(event_name)
        listener.public_send(event_name, self, *args)
      end
    end
  end
end

The test code:

describe ObservableJob do
  it "provides notification on job success" do
    notifier = stub{:on_success}
    notifier.should_receive(:on_success)
    ObservableJob.new_with_listener(notifier).success
  end
end

I'm currently using a mock expectation to ensure that my listener objects are called back correctly. This works fine. However, I tend to prefer that my tests have explicit assertions rather than relying on implicit mock expectations. Is it possible to use an explicit assertion here?

Était-ce utile?

La solution

After doing some more research on this, it is not possible to use an explicit assertion in this case.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top