【发布时间】:2016-09-16 10:21:10
【问题描述】:
我有自己的宝石(仅供学习)。所以,我有我的测试应用程序,它使用我的 gem。在我的应用程序中,我创建了初始化文件,用于为我的 gem 模型添加自定义回调。
类似这样的:
#my_app/confing/initializers/my_nagios.rb
MyNagios::Check.class_eval do
after_save :send_slack_notification
private
def send_slack_notification
...
@notifier ||= Slack::Notifier.new "..."
...
@notifier.ping "..."
现在我想为这个自定义回调创建 Rspec 测试。
我尝试这样的事情:
describe MyNagios::Check do
let(:slack_notifier) { double(Slack::Notifier) }
it 'should send notification' do
... some actions ...
expect(slack_notifier).to receive(:ping).with('test') # 'test' just for now, it will be replaced
但我收到一个错误:
(Double Slack::Notifier).ping("test")
expected: 1 time with arguments: ("test")
received: 0 times
我应该使用double 代替Slack::Notifier 吗?我的测试出了什么问题?
【问题讨论】:
标签: ruby-on-rails callback rubygems rspec-rails