1

While writing a unit test for following:

def foo()
  popen_response = ""
  IO.popen(@packaging_cmd, :err=>[:child, :out]) {|io| popen_response = io.read }
  rc = $?
  @log.debug{"Response from IO.popen() : #{popen_response}. rc: '#{rc}'"}
  if rc.exitstatus != 0
    @log.error{"Packaging failed. rc: '#{rc}'"}
    raise PackagingError.new("Packaging failed. rc: '#{rc}'")
  end
end

I'm stuck because I don't know how to mock/stub what $? evaluates to. I can hack around by creating a function that returns $? and mock that function or tinker with teh command passed to IO.popen(), but I wonder if there is any official way in RR that I can use.

I'm using rr with stock Test::Unit::TestCase

require 'test/unit'
require 'rr'
Kashyap
  • 15,354
  • 13
  • 64
  • 103

1 Answers1

1

I'm not sure which mocking framework you are using, but in RSpec:

allow_any_instance_of(Process::Status).to receive(:exitstatus).and_return(0)
B Seven
  • 44,484
  • 66
  • 240
  • 385