Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在ActiveRecord模型中测试推送器通道是否触发_Ruby On Rails_Activerecord_Rspec_Mocking_Pusher - Fatal编程技术网

Ruby on rails 如何在ActiveRecord模型中测试推送器通道是否触发

Ruby on rails 如何在ActiveRecord模型中测试推送器通道是否触发,ruby-on-rails,activerecord,rspec,mocking,pusher,Ruby On Rails,Activerecord,Rspec,Mocking,Pusher,我在模型中有以下方法: 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_rece

我在模型中有以下方法:

  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

我没有做什么?

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

首先重构

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.