views:

387

answers:

3

I know the short answer is Mocks, but any examples would be good. I need to be able to test the following: 1. Connect/disconnect 2. Receive data at set intervals 3. Pause in data transmission, causing my class to attempt reconnect 4. Test that events are firing when expected.

As a start, I was thinking of defining an interface, which would use a stream, which would allow me to simply get my class to connect to any stream, which I could control better than a serial port, and would allow me to do it programmaticly. If anyone has a better idea, I'd greatly appreciate it.

A: 

http://en.wikipedia.org/wiki/COM_port_redirector lists some free / open source virtual COM port drivers / redirectors, which can be helpful for your testing!

Khurram Aziz
Thanks Khurram, I've already got a virtual serial driver, which works well, but requires me to manually test the behavior of my port. While I could automate opening the 'buddy' port and sending data, I'd rather fund a cleaner way of doing it. Unless I've misunderstood your suggestion.
A: 

I've used the below successfully, but only to test processing of the data, and internal timings. This cannot cope with the TestingClass closing/opening the SerialPort itself.

        using (NamedPipeServerStream input = new NamedPipeServerStream("Test", PipeDirection.InOut))
        using (NamedPipeClientStream pipeClient = new NamedPipeClientStream("Test"))
        using (MemoryStream output = new MemoryStream())
        using (StreamReader inSerial = new StreamReader(pipeClient))
        using (StreamWriter outSerial = new StreamWriter(svpConsumer))
        {
            StartPipeServer(input);
            pipeClient.Connect();
            using (TestingClass myTest = new TestingClass(onSerial, outSerial))
            {
               input.Write(...);
               input.Flush(...);
               Assert on checking output
            }
        }

where:

    internal void StartPipeServer(NamedPipeServerStream pipeServer)
    {
        Thread thread = new Thread(WaitForConnections);
        thread.Start(pipeServer);
    }

    internal void WaitForConnections(object o)
    {
        NamedPipeServerStream pipe = (NamedPipeServerStream)o;
        pipe.WaitForConnection();
    }

HTH, RiP

Richard
A: 

Sounds like you want to perform integration testing rather than unit testing?

If you mean unit testing, you could:

  1. Create a wrapper around the serial port
  2. Give the wrapper an interface, perhaps IPort
  3. Pass this into the class that requires a SerialPort
  4. Mock the SerialPost that is passed in

    internal interface IPort { void Connect(); //Other members }

    internal class SerialPort : IPort { public void Connect() { //Implementation } }

    public class DataRetriever { private IPort _port; public DataRetriever(IPort port) { _port = port; }

    public void ReadData() { _port.Connect(); } }

Now you can test the Data Retriever class. Unfortinatly when you get close to the framework (such as the SerialPort wrapper), you are unable to unit test it. You will need to leave that to integration tests.

David Kiff