Julien
Julien

Reputation: 5779

How to use mocha outside of unit tests?

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)

Upvotes: 1

Views: 322

Answers (1)

Mario Visic
Mario Visic

Reputation: 2663

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

Upvotes: 2

Related Questions