2011-12-23 2 views
1

私はユニットテストの外で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) 

答えて

2

私はこのためfakewebの宝石を使用してお勧めします。これは、HTTP要求をスタブアウトするように設計されています。

require 'rubygems' 
require 'fakeweb' 

FakeWeb.register_uri(:get, "http://something.com/", :body => "", :status => ["500", "Server Error"]) 

さらに詳しい情報:https://github.com/chrisk/fakeweb

関連する問題