如何在单元测试之外使用 mocha?
我试图在单元测试之外使用 mocha 来模拟 Net::HTTPResponse 对象。这是一个简单的例子:
#!/usr/bin/env ruby -w
require 'net/http'
require 'rubygems'
require 'mocha'
response = mock('Net::HTTPResponse')
response.stubs(:code => '500', :message => "Failed", :content_type => "text/plaint", :body => '')
我收到此错误:
undefined method `mock' for main:Object (NoMethodError)
I'm trying to use mocha outside of unit tests to mock an Net::HTTPResponse object. here is a simple example:
#!/usr/bin/env ruby -w
require 'net/http'
require 'rubygems'
require 'mocha'
response = mock('Net::HTTPResponse')
response.stubs(:code => '500', :message => "Failed", :content_type => "text/plaint", :body => '')
I get this error:
undefined method `mock' for main:Object (NoMethodError)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我建议为此使用 fakeweb gem。它旨在阻止 http 请求。
更多信息:https://github.com/chrisk/fakeweb
I'd recommend using the fakeweb gem for this. It's designed to stub out http requests.
More info: https://github.com/chrisk/fakeweb