Rspec should_receive对扩展self的模块不起作用

时间:2011-12-25 06:59:34

标签: ruby rspec

经过测试的模块

module Notifications
  extend self

  def notify(push_id, platform, message, event, args)
    puts "hello"
    ...     
  end
end


Notifications.should_receive(:notify)

Rspec表示没有调用通知,但是“hello”会打印到日志中。

1 个答案:

答案 0 :(得分:0)

它适用于我:

module Notifications
  extend self

  def notify(push_id, platform, message, event, args)
    puts "hello"
  end
end


describe 'RSpec stubbing' do
  it "works when I don't stub" do
    Notifications.should_not_receive(:notify)
  end

  specify 'works when I do stub' do
    Notifications.should_receive(:notify)
    Notifications.notify(1,2,3,4,5)
  end
end

但是,您的代码显然是错误的,因为当您执行#should_receive时,它不会调用原始方法。如果您可以看到打印出“hello”,那么它不会被RSpec捕获。可能你对是否存在实例或模块本身感到困惑。