From: Jim Weirich Date: 2003-06-05T21:32:02+09:00 Subject: Re: A unit testing problem... On Thu, 2003-06-05 at 01:58, Hal E. Fulton wrote: > But of course, interactive tests are not good in > the long term. Agreed. > How would you test this? For my first pass, I > didn't have to make any changes to my X10 class; > I just substituted a fake SerialPort class. I assume in your tests you can script the entire exchange. In other words, when you wish to test a scenario, you know exactly what you will send and when repsonses you will get back. If that's the case, you could do something like this ... def test_startup_protocol port = MockSerialPort.new( :put, 0x80, 0x81, :get, 0x00, 0x01, :put, 0xff) cm11a = CM11A.new(port) cm11a.startup end The MockSerialPort object might look something like this ... class MockSerialPort include Test::Unit::Assertions def initialize(*script) @mode = :put @script = script end def putc(byte) expected = get_next assert_equal :put, @mode assert_equal expected, byte, "while putting" end def getc expected = get_next assert_equal :get, @mode expected end def get_next while val = @script.shift case val when :put @mode = val when :get @mode = val when Integer return val when nil return val end end end end -- -- Jim Weirich jweirich@one.net http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)