How to disable HTTP request worldwide using Test :: Unit?

How can I drown out an HTTP request like this in the twitter api below in the global scope so that it is valid for all tests in the Test :: Unit set?

stub_request(:get, "https://api.twitter.com/1/users/show.json?screen_name=digiberber"). with(:headers => {'Accept'=>'application/json', 'User-Agent'=>'Twitter Ruby Gem 1.1.2'}). to_return(:status => 200, :body => "", :headers => {}) 

This WebMock stub works in the configuration block of the TestCase () subclass, for example

 class MyTest < ActiveSupport::TestCase setup do stub_request(...)... end end 

But it is not recognized if I put it in a global setting in TestCase itself:

 require 'webmock/test_unit' class ActiveSupport::TestCase setup do stub_request(...) end end 

Which gives me an error:

 NoMethodError: undefined method `stub_request' for ActiveSupport::TestCase:Class 

I also tried fixing the def method myself

 def self.setup stub_request(...) end 

but it doesn’t work either.

Something similar happens when I use FlexMock instead of WebMock. This seems to be a problem of scale, but I cannot figure out how to get around it. Ideas?

+4
source share
3 answers

This post on different ways to configure () and teardown () made me just do

 class ActiveSupport::TestCase def setup stub_request(...) end end 

did not think to declare it as an instance method .: P

+1
source

Using FakeWeb , you can do something like this:

In * test / test_helper.rb *

 require 'fakeweb' class ActiveSupport::TestCase def setup # FakeWeb global setup FakeWeb.allow_net_connect = false # force an error if there are a net connection to other than the FakeWeb URIs FakeWeb.register_uri(:get, "https://api.twitter.com/1/users/show.json?screen_name=digiberber", :body => "", :content_type => "application/json") end def teardown FakeWeb.allow_net_connect = true FakeWeb.clean_registry # Clear all registered uris end end 

With this, you can call registered fakeweb from any test page.

+2
source

The capybara Akephalos driver does support wrapping http calls. They call it filters.

http://oinopa.com/akephalos/filters.html

http://github.com/Nerian/akephalos

0
source

All Articles