mercredi 7 septembre 2016

ruby rr: How to stub/mock built-in variables like $?

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 $?.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'

Aucun commentaire:

Enregistrer un commentaire