Help for stubbing class method with unexpected arguments

18 views
Skip to first unread message

kain

unread,
Feb 12, 2013, 3:56:35 AM2/12/13
to rs...@googlegroups.com
Hello,
sorry for the bad title. I have this simple class:

class Push
  def self.fire(event, channel, payload = nil)
    channel, event = channel.to_s, event.to_s
    $redis.publish('dispatch', prepare_packet(channel, event, payload))
  end
  ...

Before that I was using method_missing to do Push.fire_my_event(channel, payload) and map the capture of fire_(.*) as the event, however in the effort of reducing CPU time I decided to switch to a more traditional approach that you see above: Push.fire(:event, 'channel', payload).

Now, the problem is that it was easy for me to write in a single test something like this:

        it "should call correct methods for push engine" do
          Push.should_receive(:fire_media_destroy).with(subject.account.private_comet_channel, { media_file: subject.to_websocket }).once

and automatically all the other Push calls would get ignored.

Now if I write:

        it "should call correct methods for push engine" do
          Push.should_receive(:fire).with(:media_destroy, subject.account.private_comet_channel, { media_file: subject.to_websocket }).once

it fails because the Push.fire is employed in many parts of the app, and it's code that it's run well before the test, with other parameters.

Can as_null_object help here without rewriting the whole test suite?

Another small issue what I found is that rspec is swallowing exceptions. For example the error:

<Push (class)> received :fire with unexpected arguments

is written only in the logs. Sometimes I see tests passes but if I look at the logs the exception is there, this is especially dangerous when using should_receive(:arg).never

kain

unread,
Feb 12, 2013, 2:42:50 PM2/12/13
to rs...@googlegroups.com
Just went with a define_singleton_method.
Reply all
Reply to author
Forward
0 new messages