I had a few problems getting WebMock working with my rails 3 setup.
When I used webmock in my rails 3 app, the tests ran and all was nice.
But when I wanted to create a plugin using the exact same code as I just tested inside my rails app, I went into some problems.
To start, I created a new plugin using
rails generate plugin myclient
The files were generated and all seemed fine.
I then wanted to use the webmock plugin, since my client relied on some external services. (Which a test really shouldn’t do)
I had a really simple test case, just to get started:
require 'test_helper'
require 'webmock/test_unit'
require 'curb'
class MyClientTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
test 'webmock testing' do
WebMock.stub_request(:any, 'www.example.com').to_return(:body => 'tester')
data = Curl::Easy.perform("www.example.com")
p data.body_str
assert data.body_str == 'tester'
end
end
When I ran “rake test” the test failed and the body that was returned, was the body of the actual page at www.example.com.
After bashing at the problem for a long time, I found out, that webmock doesn’t work if webmock is required before curb.
So… This was just a note about something that really took up too much of my time, and… yes, I should have know that webmock couldn’t “overwrite” the curb methods, if it was loaded in first.
Anyways, the final code that works!
require 'test_helper'
require 'curb'
require 'webmock/test_unit'
class MyClientTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
test 'webmock testing' do
WebMock.stub_request(:any, 'www.example.com').to_return(:body => 'tester')
data = Curl::Easy.perform("www.example.com")
p data.body_str
assert data.body_str == 'tester'
end
end