From: Brian Candler Date: 2010-05-11T20:13:35+09:00 Subject: Re: Unit Test of method calling system() - how? Martin Hansen wrote: > @Brian > > I have been looking at flexmock I used to use flexmock, but switched to mocha. The error messages were much more informative, especially in terms of expectations not met or unexpected invocations. > but I guess the mocking method is not > important - it is how?!? ... Especially since these are private methods. Sorry, I don't understand you're trying to say there. 'system' is a private method inherited from Kernel, but you can happily mock or stub it within a test. If you know the appropriate object instance, then obj.expects(:system).... is fine. If you don't, but you know what class it is, then Foo.any_instance.expects(:system).... Unfortunately, Object.any_instance. doesn't do the trick. > Perhaps the code should be rearranged? That's another option - for example, make print_wiki a method, which you can mock out. Or you could check for the required side-effects of print_wiki, by running the test inside IO.popen, and checking the text written. This will be slower, because you're actually running the external print_wiki command. class Foo def bar system("cat /etc/motd") end end require 'test/unit' require 'mocha' class TestFoo < Test::Unit::TestCase def test_foo IO.popen("-") do |child| if child assert_match /Linux/, child.read else a = Foo.new a.bar end end end end -- Posted via http://www.ruby-forum.com/.