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
Hi Kevin, Can you supply the relevant bit of the GoogleSearch class definition? i.e. the initialize method and any associated methods. Also it looks like you are using the latest Mocha gem - is that correct? -- James. http://blog.floehopper.org
GoogleSearch looks something like this (it''s on another machine) class GoogleSearch attr_accessor :body def initialize(query_string) open("http://google.com/search?q=#{CGI::escape(query_string)}") end end On 12/21/06, James Mead <jamesmead44 at gmail.com> wrote:> Hi Kevin, > > Can you supply the relevant bit of the GoogleSearch class definition? > i.e. the initialize method and any associated methods. > > Also it looks like you are using the latest Mocha gem - is that correct? > -- > James. > http://blog.floehopper.org > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >-- Kevin Clark http://glu.ttono.us
yes, latest mocha On 12/21/06, Kevin Clark <kevin.clark at gmail.com> wrote:> GoogleSearch looks something like this (it''s on another machine) > > class GoogleSearch > attr_accessor :body > > def initialize(query_string) > open("http://google.com/search?q=#{CGI::escape(query_string)}") > end > end > > On 12/21/06, James Mead <jamesmead44 at gmail.com> wrote: > > Hi Kevin, > > > > Can you supply the relevant bit of the GoogleSearch class definition? > > i.e. the initialize method and any associated methods. > > > > Also it looks like you are using the latest Mocha gem - is that correct? > > -- > > James. > > http://blog.floehopper.org > > _______________________________________________ > > mocha-developer mailing list > > mocha-developer at rubyforge.org > > http://rubyforge.org/mailman/listinfo/mocha-developer > > > > > -- > Kevin Clark > http://glu.ttono.us >-- Kevin Clark http://glu.ttono.us
Sorry for the delayed response. I''ve managed to reproduce your error. I think it should be solved if you set the expectation on GoogleSearch instead of Object... GoogleSearch.any_instance.expects(:open) etc etc -- James. http://blog.floehopper.org