Pergunta

My task is implemented like this:

class MyTask
    @queue = :queue_name
    include BaseTask
end

module BaseTask
    include Observable
    def perform
        add_observer(MyObserver.new)
        ...
        changed
        notify_observers
    end
end

I have a Resque task that include Observable with the perform method calling add_observer, changed, and notify_observers. How do I test BaseTask with RSpec? I initialized MyTask and called task.perform. I tried Observable.should_receive(:add_observer), MyTask.any_instance.should_receive(:add_observer), BaseTask.should_receive(:add_observer), but they aren't working?

Foi útil?

Solução

You should be able to check message expectations on an instance of MyTask. For example,

it "is observable" do
  task = MyTask.new
  task.should_receive :add_observer
  task.should_receive :changed
  task.should_receive :notify_observers

  task.perform
end

Ideally, I wouldn't do it this way since a lot of message expectations on the object under test are being made. Instead, I might check some external state that is a result of calling perform.

EDIT If you want to check instance methods on MyObserver, you could do something like this, before calling perform:

observer = mock
MyObserver.stub(:new).and_return(observer)
observer.should_receive(:some_method) #replace `some_method` with whatever instance method you want
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top