[rspec-users] How to intercept an instance method from StdLib (1.9.2)

Hedge Hog hedgehogshiatus at gmail.com
Sat Mar 5 07:06:31 EST 2011


On Fri, Mar 4, 2011 at 8:12 PM, Matt Wynne <matt at mattwynne.net> wrote:
>
> On 4 Mar 2011, at 05:45, Hedge Hog wrote:
>
>> Hi,
>> I'm struggling with something that seems to be simple, and I've not
>> had any joy following the RSpec books suggestions (p. 187).
>> I'd like to test that a method raises and error when a file is not found.
>> I've tried adding this in my example just before I call my method, but
>> it never seems to get invoked.
>> Pathname.stub(:exist?).and_return(false)
>
> That's stubbing a class method.
>
> exist? is an instance method so you need to stub the specific instance of pathname that's being used in your object-under-test, rather than the Pathname class.
>

Is there a (clean) way to intercept this instance and add the
stub/mock to it?  What I see in the Rspec books suggests I'd have to
force the Pathname instance to the surface, as some method argument.
That feels all wrong - to have to change my method's interface just to
test a behavior.

Appreciate any experience, comments or suggestions people may have.

>>
>> The whole example:
>>
>>    it "should raise an error if RVM's install root does not exist" do
>>       Pathname.stub(:exist?).and_return(false)
>>       lambda{ B3::Bdd::Helpers.rvm_path}.should
>> raise_error(RuntimeError, "File not found:")
>>    end
>>
>> Appreciate any tips.
>>
>> --
>> πόλλ' οἶδ ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα
>> [The fox knows many things, but the hedgehog knows one big thing.]
>>   Archilochus, Greek poet (c. 680 BC – c. 645 BC)
>> http://wiki.hedgehogshiatus.com
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>
> cheers,
> Matt
>
> matt at mattwynne.net
> 07974 430184
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



-- 
πόλλ' οἶδ ἀλώπηξ, ἀλλ' ἐχῖνος ἓν μέγα
[The fox knows many things, but the hedgehog knows one big thing.]
  Archilochus, Greek poet (c. 680 BC – c. 645 BC)
http://wiki.hedgehogshiatus.com


More information about the rspec-users mailing list