# How to test %x\[\] calls?

**URL:** <https://www.ruby-forum.com/t/how-to-test-x-calls/171595>\
**Category:** Ruby\
**Created:** [July 6, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/how-to-test-x-calls/171595 "2009-07-06T13:26:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [July 6, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/how-to-test-x-calls/171595/1 "2009-07-06T13:26:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [July 6, 2009, 5:21pm UTC](https://www.ruby-forum.com/t/how-to-test-x-calls/171595/2 "2009-07-06T17:21:36Z")

</div>

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
> 
> >

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [July 6, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/how-to-test-x-calls/171595/3 "2009-07-06T17:26:03Z")

</div>

… 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.

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [July 6, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/how-to-test-x-calls/171595/4 "2009-07-06T19:25:09Z")

</div>

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! 🙂
