[mocha-developer] Stubbing Kernel#open

Kevin Clark kevin.clark at gmail.com
Wed Dec 20 23:47:19 EST 2006


Anyone know how to stub Kernel#open? I'm trying to mock/stub an
open-uri call, but it doesn't seem to like it.

Here's the test code, and the failures:

    body = File.open(File.dirname(__FILE__) +
'/../fixtures/google_search_california.html').read
    Object.any_instance.expects(:open).with('http://www.google.com/search?q=california').returns(stub(:body
=> body))
    @response = GoogleSearch.new('california')
    assert_equal body, @response.body


  1) Failure:
test_new(GoogleSearchTest)
    [(eval):1:in `open'
     /Users/kev/code/vanna/config/../lib/google_search.rb:5:in `initialize'
     ./test/unit/google_search_test.rb:7:in `test_new']:
Unexpected message :open('http://www.google.com/search?q=california')
sent to #<Mocha::Mock: 20045138>


  2) Failure:
test_new(GoogleSearchTest) [./test/unit/google_search_test.rb:6]:
:open('http://www.google.com/search?q=california'): expected calls: 1,
actual calls: 0




-- 
Kevin Clark
http://glu.ttono.us


More information about the mocha-developer mailing list