RSpec:如何在静态方法上使用 should_receive? [英] RSpec: how to use should_receive on a static method?
本文介绍了RSpec:如何在静态方法上使用 should_receive?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 lib/gcm.rb 中有一个模块:
I have a module in lib/gcm.rb :
require "net/http"
require "uri"
module GCM
def self.dispatch_message(reg_ids, data)
url = URI.parse(GCM_URL + "/send")
@msg = { :registrationIds => reg_ids, :data => data }
request = Net::HTTP::Post.new(url.path)
request.content_type = 'application/json'
request.body = @msg.to_json
response = Net::HTTP.start(url.host, url.port) { |http| http.request(request) }
end
end
我想测试在我的一个控制器中调用了 dispatch_message
:
I want to test that the dispatch_message
is invoked in one of my controllers:
it "should dispatch a GCM message" do
post :create, :post => @attr, :format => :json
GCM.should_receive(:dispatch_message)
end
但它失败了:
PostsController POST 'create' should dispatch a GCM message
Failure/Error: GCM.should_receive(:dispatch_message)
(GCM).dispatch_message(any args)
expected: 1 time
received: 0 times
如果重要的话,我已经禁用了与 WebMock 的网络连接.
I have disabled network connections with WebMock if it matters.
我在这里错过了什么?
推荐答案
您的期望必须在之前提出请求:
Your expectation must come before the request is made:
it "should dispatch a GCM message" do
GCM.should_receive(:dispatch_message)
post :create, :post => @attr, :format => :json
end
这篇关于RSpec:如何在静态方法上使用 should_receive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文