gpt4 book ai didi

ruby-on-rails - RSpec:如何在静态方法上使用 should_receive?

转载 作者:行者123 更新时间:2023-12-03 16:20:23 25 4
gpt4 key购买 nike

我在 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在我的一个 Controller 中调用:
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 的网络连接。

我在这里想念什么?

最佳答案

您的期望必须在提出请求之前出现:

it "should dispatch a GCM message" do
GCM.should_receive(:dispatch_message)
post :create, :post => @attr, :format => :json
end

关于ruby-on-rails - RSpec:如何在静态方法上使用 should_receive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12797264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com