This drives me insane on a regular basis. How does one mock
system(''blah'') or `blah` ?
Adding expectations on Kernel doesn''t do it. Adding expectations on
Object just makes me sad:
Object.any_instance.expects(:system).with(''ls'')
# => #<Mock:0x12b584e>.system(''ls'') - expected
calls: 0, actual calls: 1
And this really shouldn''t work (and doesn''t):
Object.expects(:system).with(''ls'')
# => #<Mock:0x12f287a>.system(''ls'') - expected
calls: 1, actual calls: 0
Anyone have a trick they like?
--
Kevin Clark
http://glu.ttono.us
On 11 Jun 2007, at 23:19, Kevin Clark wrote:> This drives me insane on a regular basis. How does one mock > system(''blah'') or `blah` ? > > Adding expectations on Kernel doesn''t do it. Adding expectations on > Object just makes me sad: > > Object.any_instance.expects(:system).with(''ls'') > # => #<Mock:0x12b584e>.system(''ls'') - expected calls: 0, actual > calls: 1 > > And this really shouldn''t work (and doesn''t): > > Object.expects(:system).with(''ls'') > # => #<Mock:0x12f287a>.system(''ls'') - expected calls: 1, actual > calls: 0 > > Anyone have a trick they like?system etc... are in a module that is included most places, so if we have class Foo def my_method_calling_system system(''echo "hello world"'') end end Then you need to do something like f = Foo.new f.expects(:system). f.my_method_calling_system I would have expected Object.any_instance.expects(:system) to work but a cursory attempt suggests it doesn;t Fred
Bah, turned out to be a Merb reloading bug. Nevermind. On 6/11/07, Frederick Cheung <fred at 82ask.com> wrote:> > On 11 Jun 2007, at 23:19, Kevin Clark wrote: > > > This drives me insane on a regular basis. How does one mock > > system(''blah'') or `blah` ? > > > > Adding expectations on Kernel doesn''t do it. Adding expectations on > > Object just makes me sad: > > > > Object.any_instance.expects(:system).with(''ls'') > > # => #<Mock:0x12b584e>.system(''ls'') - expected calls: 0, actual > > calls: 1 > > > > And this really shouldn''t work (and doesn''t): > > > > Object.expects(:system).with(''ls'') > > # => #<Mock:0x12f287a>.system(''ls'') - expected calls: 1, actual > > calls: 0 > > > > Anyone have a trick they like? > > system etc... are in a module that is included most places, so if we > have > > class Foo > def my_method_calling_system > system(''echo "hello world"'') > end > end > > Then you need to do something like > > f = Foo.new > f.expects(:system). > f.my_method_calling_system > > > I would have expected Object.any_instance.expects(:system) to work > but a cursory attempt suggests it doesn;t > > Fred > > > > > > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >-- Kevin Clark http://glu.ttono.us
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
Thanks
--- Original Message ---> Bah, turned out to be a Merb reloading bug. Nevermind.
>
> On 6/11/07, Frederick Cheung <fred at 82ask.com> wrote:
> >
> > On 11 Jun 2007, at 23:19, Kevin Clark wrote:
> >
> > > This drives me insane on a regular basis. How does one mock
> > > system(''blah'') or `blah` ?
> > >
> > > Adding expectations on Kernel doesn''t do it. Adding
expectations on
> > > Object just makes me sad:
> > >
> > > Object.any_instance.expects(:system).with(''ls'')
> > > # => #<Mock:0x12b584e>.system(''ls'')
- expected calls: 0, actual
> > > calls: 1
> > >
> > > And this really shouldn''t work (and doesn''t):
> > >
> > > Object.expects(:system).with(''ls'')
> > > # => #<Mock:0x12f287a>.system(''ls'')
- expected calls: 1, actual
> > > calls: 0
> > >
> > > Anyone have a trick they like?
> >
> > system etc... are in a module that is included most places, so if we
> > have
> >
> > class Foo
> > def my_method_calling_system
> > system(''echo "hello world"'')
> > end
> > end
> >
> > Then you need to do something like
> >
> > f = Foo.new
> > f.expects(:system).
> > f.my_method_calling_system
> >
> >
> > I would have expected Object.any_instance.expects(:system) to work
> > but a cursory attempt suggests it doesn;t
> >
> > Fred
> >
> >
> >
> >
> >
> > _______________________________________________
> > mocha-developer mailing list
> > mocha-developer at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/mocha-developer
> >
>
>
> --
> Kevin Clark
> http://glu.ttono.us
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer
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 wrongI 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.
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 >-- Kevin Clark http://glu.ttono.us
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
>>
>
>
On Jun 11, 2007, at 11:18 PM, 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 >Try adding double quotes around the backtick like this: .expects(:"`").with("ls") -Jonathan> --- Original Message --- >> Bah, turned out to be a Merb reloading bug. Nevermind. >> >> On 6/11/07, Frederick Cheung <fred at 82ask.com> wrote: >>> >>> On 11 Jun 2007, at 23:19, Kevin Clark wrote: >>> >>>> This drives me insane on a regular basis. How does one mock >>>> system(''blah'') or `blah` ? >>>> >>>> Adding expectations on Kernel doesn''t do it. Adding expectations on >>>> Object just makes me sad: >>>> >>>> Object.any_instance.expects(:system).with(''ls'') >>>> # => #<Mock:0x12b584e>.system(''ls'') - expected calls: 0, actual >>>> calls: 1 >>>> >>>> And this really shouldn''t work (and doesn''t): >>>> >>>> Object.expects(:system).with(''ls'') >>>> # => #<Mock:0x12f287a>.system(''ls'') - expected calls: 1, actual >>>> calls: 0 >>>> >>>> Anyone have a trick they like? >>> >>> system etc... are in a module that is included most places, so if we >>> have >>> >>> class Foo >>> def my_method_calling_system >>> system(''echo "hello world"'') >>> end >>> end >>> >>> Then you need to do something like >>> >>> f = Foo.new >>> f.expects(:system). >>> f.my_method_calling_system >>> >>> >>> I would have expected Object.any_instance.expects(:system) to work >>> but a cursory attempt suggests it doesn;t >>> >>> Fred >>> >>> >>> >>> >>> >>> _______________________________________________ >>> mocha-developer mailing list >>> mocha-developer at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/mocha-developer >>> >> >> >> -- >> Kevin Clark >> http://glu.ttono.us >> _______________________________________________ >> mocha-developer mailing list >> mocha-developer at rubyforge.org >> http://rubyforge.org/mailman/listinfo/mocha-developer > > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer
I could have sworn I tried that. Is it working for you?> Try adding double quotes around the backtick like this: > > .expects(:"`").with("ls") > > -Jonathan > > > > --- Original Message --- > >> Bah, turned out to be a Merb reloading bug. Nevermind. > >> > >> On 6/11/07, Frederick Cheung <fred at 82ask.com> wrote: > >>> > >>> On 11 Jun 2007, at 23:19, Kevin Clark wrote: > >>> > >>>> This drives me insane on a regular basis. How does one mock > >>>> system(''blah'') or `blah` ? > >>>> > >>>> Adding expectations on Kernel doesn''t do it. Adding expectations on > >>>> Object just makes me sad: > >>>> > >>>> Object.any_instance.expects(:system).with(''ls'') > >>>> # => #<Mock:0x12b584e>.system(''ls'') - expected calls: 0, actual > >>>> calls: 1 > >>>> > >>>> And this really shouldn''t work (and doesn''t): > >>>> > >>>> Object.expects(:system).with(''ls'') > >>>> # => #<Mock:0x12f287a>.system(''ls'') - expected calls: 1, actual > >>>> calls: 0 > >>>> > >>>> Anyone have a trick they like? > >>> > >>> system etc... are in a module that is included most places, so if we > >>> have > >>> > >>> class Foo > >>> def my_method_calling_system > >>> system(''echo "hello world"'') > >>> end > >>> end > >>> > >>> Then you need to do something like > >>> > >>> f = Foo.new > >>> f.expects(:system). > >>> f.my_method_calling_system > >>> > >>> > >>> I would have expected Object.any_instance.expects(:system) to work > >>> but a cursory attempt suggests it doesn;t > >>> > >>> Fred > >>> > >>> > >>> > >>> > >>> > >>> _______________________________________________ > >>> mocha-developer mailing list > >>> mocha-developer at rubyforge.org > >>> http://rubyforge.org/mailman/listinfo/mocha-developer > >>> > >> > >> > >> -- > >> Kevin Clark > >> http://glu.ttono.us > >> _______________________________________________ > >> mocha-developer mailing list > >> mocha-developer at rubyforge.org > >> http://rubyforge.org/mailman/listinfo/mocha-developer > > > > _______________________________________________ > > mocha-developer mailing list > > mocha-developer at rubyforge.org > > http://rubyforge.org/mailman/listinfo/mocha-developer > > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >-- Kevin Clark http://glu.ttono.us
On Jun 12, 2007, at 12:58 PM, Kevin Clark wrote:> I could have sworn I tried that. Is it working for you? >Yup, it''s working fine for me. # foo_test.rb require ''rubygems'' require ''test/unit'' require ''mocha'' class Foo def files `ls` end end class FooTest < Test::Unit::TestCase def test_should_return_files foo = Foo.new foo.expects(:"`").with("ls").returns("list of files") assert_equal "list of files", foo.files end end megiddo:~/Projects jonathan$ ruby foo_test.rb Loaded suite foo_test Started . Finished in 0.000801 seconds. 1 tests, 2 assertions, 0 failures, 0 errors megiddo:~/Projects jonathan$ ruby foo_test.rb Loaded suite foo_test Started . Finished in 0.000764 seconds. 1 tests, 2 assertions, 0 failures, 0 errors megiddo:~/Projects jonathan$ -Jonathan>> Try adding double quotes around the backtick like this: >> >> .expects(:"`").with("ls") >> >> -Jonathan >> >> >>> --- Original Message --- >>>> Bah, turned out to be a Merb reloading bug. Nevermind. >>>> >>>> On 6/11/07, Frederick Cheung <fred at 82ask.com> wrote: >>>>> >>>>> On 11 Jun 2007, at 23:19, Kevin Clark wrote: >>>>> >>>>>> This drives me insane on a regular basis. How does one mock >>>>>> system(''blah'') or `blah` ? >>>>>> >>>>>> Adding expectations on Kernel doesn''t do it. Adding >>>>>> expectations on >>>>>> Object just makes me sad: >>>>>> >>>>>> Object.any_instance.expects(:system).with(''ls'') >>>>>> # => #<Mock:0x12b584e>.system(''ls'') - expected calls: 0, actual >>>>>> calls: 1 >>>>>> >>>>>> And this really shouldn''t work (and doesn''t): >>>>>> >>>>>> Object.expects(:system).with(''ls'') >>>>>> # => #<Mock:0x12f287a>.system(''ls'') - expected calls: 1, actual >>>>>> calls: 0 >>>>>> >>>>>> Anyone have a trick they like? >>>>> >>>>> system etc... are in a module that is included most places, so >>>>> if we >>>>> have >>>>> >>>>> class Foo >>>>> def my_method_calling_system >>>>> system(''echo "hello world"'') >>>>> end >>>>> end >>>>> >>>>> Then you need to do something like >>>>> >>>>> f = Foo.new >>>>> f.expects(:system). >>>>> f.my_method_calling_system >>>>> >>>>> >>>>> I would have expected Object.any_instance.expects(:system) to work >>>>> but a cursory attempt suggests it doesn;t >>>>> >>>>> Fred >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> _______________________________________________ >>>>> mocha-developer mailing list >>>>> mocha-developer at rubyforge.org >>>>> http://rubyforge.org/mailman/listinfo/mocha-developer >>>>> >>>> >>>> >>>> -- >>>> Kevin Clark >>>> http://glu.ttono.us >>>> _______________________________________________ >>>> mocha-developer mailing list >>>> mocha-developer at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/mocha-developer >>> >>> _______________________________________________ >>> mocha-developer mailing list >>> mocha-developer at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/mocha-developer >> >> _______________________________________________ >> mocha-developer mailing list >> mocha-developer at rubyforge.org >> http://rubyforge.org/mailman/listinfo/mocha-developer >> > > > -- > Kevin Clark > http://glu.ttono.us > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer
On Jun 12, 2007, at 12:58 PM, Kevin Clark wrote:> I could have sworn I tried that. Is it working for you? >Actually after further testing the double quotes aren''t required after all. It seems the key to getting it to work is to put the expectation on the object calling the ` method rather than on Object.any_instance. -Jonathan>> Try adding double quotes around the backtick like this: >> >> .expects(:"`").with("ls") >> >> -Jonathan >> >> >>> --- Original Message --- >>>> Bah, turned out to be a Merb reloading bug. Nevermind. >>>> >>>> On 6/11/07, Frederick Cheung <fred at 82ask.com> wrote: >>>>> >>>>> On 11 Jun 2007, at 23:19, Kevin Clark wrote: >>>>> >>>>>> This drives me insane on a regular basis. How does one mock >>>>>> system(''blah'') or `blah` ? >>>>>> >>>>>> Adding expectations on Kernel doesn''t do it. Adding >>>>>> expectations on >>>>>> Object just makes me sad: >>>>>> >>>>>> Object.any_instance.expects(:system).with(''ls'') >>>>>> # => #<Mock:0x12b584e>.system(''ls'') - expected calls: 0, actual >>>>>> calls: 1 >>>>>> >>>>>> And this really shouldn''t work (and doesn''t): >>>>>> >>>>>> Object.expects(:system).with(''ls'') >>>>>> # => #<Mock:0x12f287a>.system(''ls'') - expected calls: 1, actual >>>>>> calls: 0 >>>>>> >>>>>> Anyone have a trick they like? >>>>> >>>>> system etc... are in a module that is included most places, so >>>>> if we >>>>> have >>>>> >>>>> class Foo >>>>> def my_method_calling_system >>>>> system(''echo "hello world"'') >>>>> end >>>>> end >>>>> >>>>> Then you need to do something like >>>>> >>>>> f = Foo.new >>>>> f.expects(:system). >>>>> f.my_method_calling_system >>>>> >>>>> >>>>> I would have expected Object.any_instance.expects(:system) to work >>>>> but a cursory attempt suggests it doesn;t >>>>> >>>>> Fred >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> _______________________________________________ >>>>> mocha-developer mailing list >>>>> mocha-developer at rubyforge.org >>>>> http://rubyforge.org/mailman/listinfo/mocha-developer >>>>> >>>> >>>> >>>> -- >>>> Kevin Clark >>>> http://glu.ttono.us >>>> _______________________________________________ >>>> mocha-developer mailing list >>>> mocha-developer at rubyforge.org >>>> http://rubyforge.org/mailman/listinfo/mocha-developer >>> >>> _______________________________________________ >>> mocha-developer mailing list >>> mocha-developer at rubyforge.org >>> http://rubyforge.org/mailman/listinfo/mocha-developer >> >> _______________________________________________ >> mocha-developer mailing list >> mocha-developer at rubyforge.org >> http://rubyforge.org/mailman/listinfo/mocha-developer >> > > > -- > Kevin Clark > http://glu.ttono.us > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer
Ah, must have been my reloading issue that was keeping it from working. Cool, thanks for the help guys. On 6/12/07, Jonathan Younger <daikini at gmail.com> wrote:> > On Jun 12, 2007, at 12:58 PM, Kevin Clark wrote: > > > I could have sworn I tried that. Is it working for you? > > > > Actually after further testing the double quotes aren''t required > after all. It seems the key to getting it to work is to put the > expectation on the object calling the ` method rather than on > Object.any_instance. > > -Jonathan > > >> Try adding double quotes around the backtick like this: > >> > >> .expects(:"`").with("ls") > >> > >> -Jonathan > >> > >> > >>> --- Original Message --- > >>>> Bah, turned out to be a Merb reloading bug. Nevermind. > >>>> > >>>> On 6/11/07, Frederick Cheung <fred at 82ask.com> wrote: > >>>>> > >>>>> On 11 Jun 2007, at 23:19, Kevin Clark wrote: > >>>>> > >>>>>> This drives me insane on a regular basis. How does one mock > >>>>>> system(''blah'') or `blah` ? > >>>>>> > >>>>>> Adding expectations on Kernel doesn''t do it. Adding > >>>>>> expectations on > >>>>>> Object just makes me sad: > >>>>>> > >>>>>> Object.any_instance.expects(:system).with(''ls'') > >>>>>> # => #<Mock:0x12b584e>.system(''ls'') - expected calls: 0, actual > >>>>>> calls: 1 > >>>>>> > >>>>>> And this really shouldn''t work (and doesn''t): > >>>>>> > >>>>>> Object.expects(:system).with(''ls'') > >>>>>> # => #<Mock:0x12f287a>.system(''ls'') - expected calls: 1, actual > >>>>>> calls: 0 > >>>>>> > >>>>>> Anyone have a trick they like? > >>>>> > >>>>> system etc... are in a module that is included most places, so > >>>>> if we > >>>>> have > >>>>> > >>>>> class Foo > >>>>> def my_method_calling_system > >>>>> system(''echo "hello world"'') > >>>>> end > >>>>> end > >>>>> > >>>>> Then you need to do something like > >>>>> > >>>>> f = Foo.new > >>>>> f.expects(:system). > >>>>> f.my_method_calling_system > >>>>> > >>>>> > >>>>> I would have expected Object.any_instance.expects(:system) to work > >>>>> but a cursory attempt suggests it doesn;t > >>>>> > >>>>> Fred > >>>>> > >>>>> > >>>>> > >>>>> > >>>>> > >>>>> _______________________________________________ > >>>>> mocha-developer mailing list > >>>>> mocha-developer at rubyforge.org > >>>>> http://rubyforge.org/mailman/listinfo/mocha-developer > >>>>> > >>>> > >>>> > >>>> -- > >>>> Kevin Clark > >>>> http://glu.ttono.us > >>>> _______________________________________________ > >>>> mocha-developer mailing list > >>>> mocha-developer at rubyforge.org > >>>> http://rubyforge.org/mailman/listinfo/mocha-developer > >>> > >>> _______________________________________________ > >>> mocha-developer mailing list > >>> mocha-developer at rubyforge.org > >>> http://rubyforge.org/mailman/listinfo/mocha-developer > >> > >> _______________________________________________ > >> mocha-developer mailing list > >> mocha-developer at rubyforge.org > >> http://rubyforge.org/mailman/listinfo/mocha-developer > >> > > > > > > -- > > Kevin Clark > > http://glu.ttono.us > > _______________________________________________ > > mocha-developer mailing list > > mocha-developer at rubyforge.org > > http://rubyforge.org/mailman/listinfo/mocha-developer > > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >-- Kevin Clark http://glu.ttono.us