On Dec 23, 2006, at 19:00, Michal Kwiatkowski wrote:
> I want to mock standard Kernel.system with my own method and then
> revert back to the original version. I was able to do the former, but
> not the latter. Currently I'm overwriting Kernel.system method
> definition with:
>
> def system_should_return what
>     Kernel.module_eval "def system(*args) #{what.inspect} end"
> end
>
> I don't a have idea how going back to the original state can be done.
> Thanks in advance for any help on this.

Its simpler to not overwrite Kernel#system.

If your class looks something like:

$ cat runner.rb
class Runner

   def run(command)
     puts command
     system command
   end

end

Use open classes and inheritance to add a system that works when you  
want it.  Restoring the real system for the Runner class is as simple  
as removing the method again.

$ cat test_runner.rb
require 'test/unit'
require 'runner'

class Runner
   attr_accessor :commands, :results
   def system(command)
     @commands << command
     @results.shift
   end
end

class TestRunner < Test::Unit::TestCase

   def setup
     @runner = Runner.new
     @runner.commands = []
     @runner.results = []
   end

   def test_run
     @runner.results << false

     assert_equal false, @runner.run("exit 1")

     assert @runner.results.empty?
     assert_equal 1, @runner.commands.length
     assert_equal 'exit 1', @runner.commands.first
   end

end

$ ruby test_runner.rb
Loaded suite test_runner
Started
exit 1
.
Finished in 0.000369 seconds.

1 tests, 4 assertions, 0 failures, 0 errors

If you want to get rid of the "exit 1" use util_capture from  
ZenTest's test/zentest_assertions.rb

-- 
Eric Hodel - drbrain / segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!