Reputation:
I know the short answer is Mocks, but any examples would be good. I need to be able to test the following:
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.
Upvotes: 10
Views: 7094
Reputation: 1210
Sounds like you want to perform integration testing rather than unit testing?
If you mean unit testing, you could:
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.
Upvotes: 5
Reputation: 192
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
Upvotes: 0
Reputation: 1498
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!
Upvotes: 3