我正在尝试在单元测试之外使用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)
答案 0 :(得分:2)
我建议使用fakeweb gem。它旨在存根http请求。
require 'rubygems'
require 'fakeweb'
FakeWeb.register_uri(:get, "http://something.com/", :body => "", :status => ["500", "Server Error"])