I have a peripheral driver that uses the serial port to communicate with the peripheral device. I want to unit test this driver and tried to mock the serial port. Therefore I created a wrapper for the framework type SerialPort to have it implement an interface:
public interface IScannerPort
{
Handshake Handshake { get; set; }
bool IsOpen { get; }
event SerialDataReceivedEventHandler DataReceived;
void Close( );
void Open( );
string ReadLine( );
}
Now I created a mock using moq:
Mock<IScannerPort> scannerPort = new Mock<IScannerPort>( );
I then want to raise the DataReceived
event. But SerialDataReceivedEventArgs doesn't let me set the EventType property. So I tried to mock SerialDataReceivedEventArgs as well, ending up with
Mock<SerialDataReceivedEventArgs> args = new Mock<SerialDataReceivedEventArgs>();
args.SetupProperty(a => a.EventType, SerialData.Eof);
But the second line raises a NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member: a => a.EventType
How can I raise the event? Or how can I mock the event args?
The SerialDataReceivedEventArgs doesn't necessarily be mocked. One can create an instance of it using reflection:
ConstructorInfo constructor = typeof (SerialDataReceivedEventArgs).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null,
new[] {typeof (SerialData)},
null);
SerialDataReceivedEventArgs eventArgs =
(SerialDataReceivedEventArgs)constructor.Invoke(new object[] {SerialData.Eof});
Then the event can be raised on the mocked instance using the "real" instance of the vent args:
scannerPort.Raise( s => s.DataReceived += null, eventArgs );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With