Hello…
I’m trying to figure out how to test something
properly and I’m seeking your input.
I’m talking to a device via a serial port. The
protocol is described in terms of hex, so for a
first pass I was using a sort of mock object
serial port stub:
class SerialPort < IO
attr_accessor :read_timeout, :sync
def initialize(*args)
end
def putc(ch)
STDOUT.printf "PC: 0x%02x\n", ch
end
def getc
STDOUT.print "CM11A: "
n = Integer(STDIN.gets)
exit if n==999
n
end
end
And I was pretending to be the receiving device and
talking back and forth to the PC in hex:
C:\Windows\Desktop\projects\domo>ruby x10.rb
CM11A: 0x5a
PC: 0xc3
PC: 0x2e
PC: 0x66
CM11A: 0x57
PC: 0x2e
PC: 0x66
CM11A: 0x94
PC: 0x00
But of course, interactive tests are not good in
the long term.
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 want to use StringIO, but it’s not obvious to me
how to proceed.
I thought about sending fake arguments to the
SerialPort constructor, which would then set up
StringIO objects… it was getting rather too
complex for my tastes. As I said, I don’t want
to change the caller for the sake of testing.
What do you think?
Hal
···
–
Hal Fulton
hal9000@hypermetrics.com