3

Currently using Pusher and RSpec.

Pusher.should_receive( :trigger ).with( 'message', { :data => '12345' })

This would work, except the call is Pusher[ 'channel-id' ].trigger...

How to mock this with RSpec?

4 Answers 4

9

Well [] is a function name so it can be stubbed. In the Pusher source you see: def_delegators :default_client, :webhook, :channel, :[] So all of these methods are forwarded to default_client. So this is actually a chain of methods.

I would do what you want to do like this.

 mock_client = mock('client')
 Pusher.stub(:[]).with('channel-id').and_return(mock_client)
 mock_client.should_receive( :trigger ).with( 'message', { :data => '12345' })

I do not have rspec handy right now, but see no reason why it would not work.

Sign up to request clarification or add additional context in comments.

Comments

7

Newer versions of Pusher support a shallower API that is easier to stub:

expect(Pusher).to receive(:trigger).with("channel-id", "message", { data: "12345" })

Pusher.trigger("channel-id", "message", { data: "12345" })

Comments

3

There is also pusher-fake which starts up a fake server for your tests, so you don't have to stub – it lets your app both send and receive via that fake server.

Comments

0

Michael Papile's answer is essentially correct. Here's the code I used:

Pusher.stub_chain( :[] , :trigger )
Pusher[ channel_id ].should_receive( :trigger ).with( 'message',  { :data => '12345' })

Actually, it turns out this code does not work because it does not match channel_id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.