RSpec:如何在静态方法上使用should_receive?
我在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
:
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
- 不兼容的库版本:nokogiri.bundle需要11.0.0或更高版本,但libxml2.2.dylib
- ActionDispatch :: Http :: UploadedFile.content_type未在Rspec测试中初始化
- 检查Capybara的复选框
- 如何测试before_filter在Rails中正确使用RSpec
- 如何使用rspec模拟AWS SDK(v2)?
- 使用rspec的login_as首次测试失败
- 测试在我的Ubuntu pc上运行,但不在我的Mac OS X Lion 10.7.3,Ruby 1.9.3,Rails 3.2.1上运行
- 如何使用rspec在ruby中模拟super?
- 在一切都变绿之后,如何让autospec / test不运行完整的测试套件?