Hi all,
how can I set an expectation on an external command execution?
Code:
def do_stuff(arg)
%x(external_executable_name #{arg})
end
Test (not working):
it “shells out and executes ‘external_executable_name’ with a bunch of
nifty options” do
Kernel.should_receive(:`).with(“external_executable_name argy
args”)
@worker.do_stuff(“argy args”)
end
If I change the code to:
def do_stuff(arg)
Kernel.`(external_executable_name #{arg})
end
…the test passes.
If I change the code to:
def do_stuff(arg)
(external_executable_name #{arg})
end
…the test fails.
Any other ideas? I could use Kernel#system() of course, but then I’d
lose the output (or I’d have to go through the hoops of redirecting and
reading etc, which I’d prefer to avoid). Why can’t I test for the
original %x?
I guess it’s because Kernel isn’t the receiver of the call. It’s the
top-level main object (of class Object, which mixes in Kernel)
self
=> main
self.class
=> Object
class <<self; self; end.class_eval { define_method(:`) { puts “yay!” } }
=> #Proc:0xb7c79054@:2(irb)
%x{cat /dev/null}
yay!
=> nil
… or inside your own object, it’s that object which is the receiver
(which is ultimately subclassed from Object, which mixes in Kernel)
$ cat zog.rb
class Foo
def boing(x)
puts “boing #{x.inspect}”
end
alias :"`" :boing
def bar
%x{hello}
end
end
Foo.new.bar
$ ruby zog.rb
boing “hello”
So I think you should be able to set the expectation on your own object
- as long as you can do that for private methods.
On Tue, 7 Jul 2009 00:26:05 +0900, Brian C. wrote:
def bar
%x{hello}
end
end
Foo.new.bar
$ ruby zog.rb
boing “hello”
So I think you should be able to set the expectation on your own object
- as long as you can do that for private methods.
Totally works! You rock! 