0

我在模型中有以下方法:

  def trigger_events_updated_push_event
    Pusher['events'].trigger('events_updated', {})
  end

我有以下规格:

   describe '#trigger_events_updated_push_event' do
      it 'should send message to Pusher' do
        Pusher['events'].should_receive(:trigger).with('events_updated', {})
        subject.send(:trigger_events_updated_push_event)
      end
    end

这会产生此错误:

 Failure/Error: Unable to find matching line from backtrace
       (#<Pusher::Channel:0x007ff16f18ae58>).trigger("events_updated", {})
           expected: 1 time
           received: 0 times

我没有做什么?

4

1 回答 1

-1

首先,您的代码需要改进。您在这个类中硬编码了另一个类/常量,使其紧密耦合。

首先重构

def trigger_events_updated_push_event(event=Pusher['event'])
  event.trigger('events_updated', {})
end
# This way you allow other kinds of event to be injected.

然后测试。

it "fires event" do
  event = double
  event.stub(:trigger).with(anything)
  event.should_receive(:trigger).with('events_updated', {})
  subject.trigger_events_updated_push_event
end
# Your test is only about this Class and does not depend on Pusher at all.
于 2013-08-23T05:35:21.153 回答