[mocha-developer] Mocking system/`

John Pywtorak jpywtora at calpoly.edu
Tue Jun 12 13:00:57 EDT 2007


irb(main):005:0> module Kernel
irb(main):006:1> def `(*args)
irb(main):007:2>  raise "nope, not gonna do it"
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> `ls`
RuntimeError: nope, not gonna do it
         from (irb):7:in ``'
         from (irb):10
irb(main):011:0>

That gives me hope, any ideas?


Kevin Clark wrote:
> Well, in the parse tree both %x and `` become an xstr, but I _thought_
> those became a method dispatch. Maybe it happens in the C and so can't
> be intercepted.
> 
> On 6/12/07, Paul Battley <pbattley at gmail.com> wrote:
>> On 12/06/07, jpywtora at calpoly.edu <jpywtora at calpoly.edu> wrote:
>>> Great; However, I was hoping to see how to mock the backtics, any ideas?
>>>
>>> Object.any_instance.expects(:`).with("ls")   # my guess, but is wrong
>> I don't think it's possible. `` (or %x) isn't a normal message - it's
>> a language construct of its own. (Sorry for the vague, hand-waving
>> explanation!)
>>
>> A more verbose but more flexible alternative might be to use popen or
>> open3 in your code instead of backticks. You *can* mock that.
>>
>> Paul.
>> _______________________________________________
>> mocha-developer mailing list
>> mocha-developer at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/mocha-developer
>>
> 
> 



More information about the mocha-developer mailing list