[rspec-users] Stubbing a Kernel method?

Pat Maddox pergesu at gmail.com
Fri Oct 19 12:32:26 EDT 2007

On 10/19/07, Paul Dlug <paul.dlug at gmail.com> wrote:
> Is it possible to stub a Kernel method? I'm specifically interested in
> the 'open' method to test some code using open-uri. I've tried:
> Kernel.should_receive(:open).with('filename').and_return('data')
> However, this doesn't seem to work. Any suggestions would be appreciated.
> --Paul
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Kernel gets mixed in to an object, so you need to stub it on the object itself.

describe Newsreader do
  it "should open the uri" do
    @reader = Newsreader.new
    @reader.should_receive(:open_uri).with "http://example.com/feed.xml"
    @reader.grab_feed "http://example.com/feed.xml"

class Newsreader
  def grab_feed(uri)
    response = open_uri uri


More information about the rspec-users mailing list