From: Shadowfirebird Date: 2008-08-02T07:03:32+09:00 Subject: Re: unit testing advice Many thanks. Clearly I'm going to have to go back to research mode on this one. I've found this lovely bit of example code for mocha, though -- see below. If I understand you correctly, you seem to be saying that you *don't* test the output routine; you use a mock to fake it so that you can test the rest of the code? That would imply that you farm out the difficult bits of the output routine into other methods in the same way that you would with functional programming -- ideally, leaving the output routine as something that is so simple it doesn't need testing? class Enterprise def initialize(dilithium); @dilithium = dilithium; end def go(warp_factor); warp_factor.times { @dilithium.nuke(:anti_matter) }; end end require 'test/unit' require 'rubygems' require 'mocha' class EnterpriseTest < Test::Unit::TestCase def test_should_boldly_go dilithium = mock() dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test enterprise = Enterprise.new(dilithium) enterprise.go(2) end end On Fri, Aug 1, 2008 at 10:37 PM, Gregory Brown wrote: > On Fri, Aug 1, 2008 at 5:31 PM, Shadowfirebird wrote: >> Forgive me if this is a stupid question. >> >> (Actually, I know it's a stupid question -- they're the only ones >> worth asking...) >> >> * How do you unit test a method who's job is to interface with the >> outside world? For example, a method that outputs to a file? * >> >> I've done a lot of coding, but in a dinosaur language -- automated >> unit tests are completely new to me. I understand how to use >> unit/test; but it seems to me that that's only half the story. I need >> some suggestions on how to design my code in such a way as it can be >> tested. Suggestions, anyone? > > Usually, mock objects are best for this. There exist some special > purpose ones, but it is relatively straightforward to put one together > using something like Mocha or Flexmock, both available on RubyForge. > If you are using RSpec, support for mock objects is built in. > > -greg > > -- > Killer Ruby PDF Generation named after a magnificent sea creature: > http://github.com/sandal/prawn | Non-tech stuff at: > http://metametta.blogspot.com > > -- Me, I imagine places that I have never seen / The colored lights in fountains, blue and green / And I imagine places that I will never go / Behind these clouds that hang here dark and low But it's there when I'm holding you / There when I'm sleeping too / There when there's nothing left of me / Hanging out behind the burned-out factories / Out of reach but leading me / Into the beautiful sea