1

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)
Machavity
  • 30,841
  • 27
  • 92
  • 100
Julien
  • 5,729
  • 4
  • 37
  • 60

1 Answers1

2

I'd recommend using the fakeweb gem for this. It's designed to stub out http requests.

require 'rubygems'
require 'fakeweb'

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

More info: https://github.com/chrisk/fakeweb

Mario Visic
  • 2,653
  • 1
  • 22
  • 29