Is there a way to set the value of $? in a mock in Ruby?

Posted by rleber on Stack Overflow See other posts from Stack Overflow or by rleber
Published on 2011-01-03T23:46:56Z Indexed on 2011/01/03 23:54 UTC
Read the original article Hit count: 166

Filed under:
|
|
|

I am testing some scripts that interface with system commands. Their logic depends on the return code of the system commands, i.e. the value of $?. So, as a simplified example, the script might say:

def foo(command)
  output=`#{command}`
  if $?==0
    'succeeded'
  else
    'failed'
  end
end

In order to be able to test these methods properly, I would like to be able to stub out the Kernel backquote call, and set $? to an arbitrary value, to see if I get appropriate behavior from the logic in the method after the backquote call.

I can't figure out a way to do this. (In case it matters, I'm testing using Test::Unit and Mocha.)

© Stack Overflow or respective owner

Related posts about ruby

Related posts about bash