Thanks,
But, we are actually wrapping the SerailPort object. It is the wrapper
that I would like to test. We have a "COMBuffer" class which uses the
SerialPort object. But, in order to test the COMBuffer wrapper class,
I want to mock the SerialPort class so that it would not attempt to
connect to a real serialport.
example COMBuffer method:
public void SetupSerialPort(string p_PortName, int p_BaudRate, Parity
p_Parity,
int p_DataBits, StopBits
p_StopBits, Handshake p_Handshake,
int p_ReadTimeoutMS, int
p_WriteTimeoutMS, SerialPort m_SerialPort)
{
if (string.IsNullOrEmpty(p_PortName) ||
p_BaudRate == 0 ||
p_DataBits == 0)
{
throw new ArgumentException("Invalid value passed into
Setup");
}
if (m_SerialPort == null)
{
m_SerialPort = new SerialPort()
}
m_SerialPort.PortName = p_PortName;
m_SerialPort.BaudRate = p_BaudRate;
m_SerialPort.Parity = p_Parity;
m_SerialPort.DataBits = p_DataBits;
m_SerialPort.StopBits = p_StopBits;
//Default is Handshake.None. Only change if not
Handshake.None.
if (p_Handshake != Handshake.None)
{
m_SerialPort.Handshake = p_Handshake;
}
m_ReadOperationIntervalMS = m_ReadWaitTimeMS =
p_ReadTimeoutMS;
m_WriteOperationIntervalMS = m_WriteWaitTimeMS =
p_WriteTimeoutMS;
m_SerialPort.DataReceived += new
SerialDataReceivedEventHandler(PortDataReceived);
m_SerialPort.Open();
m_SerialPort.DiscardInBuffer();
m_SerialPort.DiscardOutBuffer();
}
This is just one example, there are many more methods that I would
like to test that use the SerialPort. So, is it possible to test a
method that uses the SerialPort object?
Thanks
On Apr 30, 3:41 pm, Shane Courtrille <
shanecourtri...@gmail.com>
wrote:
> If the method is not virtual then Rhino Mocks can't mock it.
>
> In that case you have two choices. Use something else like TypeMock or
> preferably create your own wrapper around the serial port stuff.
>
> From an architectural point of view it's a good practice to wrap components
> you don't have complete control over. More important then the testing
> aspect is that you aren't coupling your code to any one vendor or product.
> If a few weeks down the rode you realize you need the power of a 3rd party
> serial port control you only have to create a new wrapper instead of going
> through your code and modifying everywhere that deals with serial ports.
>