【问题标题】:How to test whether Pusher channel is triggered in ActiveRecord model如何在 ActiveRecord 模型中测试 Pusher 通道是否被触发
【发布时间】:2013-08-23 02:44:38
【问题描述】:

我在一个模型中有以下方法:

  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

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails activerecord rspec mocking pusher


    【解决方案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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多