rubytestingrspecjobsdashing

Testing a job in Dashing


I am building an application with Dashing/Smashing right now, and I am using rspec to test my code. However, I cannot figure out how to check that send_event is called. I have tried

expect(Sinatra::Application).to receive(:send_event).twice

and

expect(Dashing).to receive(:send_event).twice,

but neither have worked. I am not sure what object is supposed to receive the call to send_event since it lies inside Dashing in app.rb. There is also this issue, talking about the same thing, unanswered on the Dashing GitHub.

Any advice on how to do this would be much appreciated. Thank you!

Update:

I still have not figured out how to do this, but I have discovered that this works:

let(:dummy_class) { Class.new { include Dashing } }

context 'something' do
    it 'does something' do
        expect(dummy_class).to receive(:send_event).once
        dummy_class.send('send_event', 'test', current: 'test')
    end
end

However, if I use the method I want to call that contains send_event as opposed to doing dummy_class.send(...), then it does not recognize that the method was called. It must have to do with my test not using the dummy class. I don't know if there is any way to get around this and make it use the dummy class.


Solution

  • I figured it out!

    Do not call send_event directly within the job. Call it within some other class, perhaps called EventSender. Then, to test that send_event is called, treat it as though it is an instance method of that class instead of a method of a module. Your code might look like this, for example:

    describe 'something' do
        context 'something' do
            it 'does something' do
                happy_es = EventSender.new(...)
                expect(happy_es).to receive(:send_event).with(...)
                happy_es.method_that_calls_sendevent
            end
        end
    end
    

    Hope this helps someone who is struggling with the same thing. :)