[英]Setting a passed parameter to a specific value in Moq
I'm unit testing a class that talks to a hardware device through a serial port. 我正在对通过串行端口与硬件设备通信的类进行单元测试。 I created an interface to isolate the SerialPort
class in System.IO: 我创建了一个接口来隔离System.IO中的SerialPort
类:
public interface ISerialPort
{
String PortName { get; set; }
bool IsOpen { get; }
void Open();
void Close();
int Read(byte[] buffer, int offset, int count);
void Write(byte[] buffer, int offset, int count);
}
There's a function in my class under test that calls Read
, and checks for specific values. 我的被测类中有一个函数调用Read
,并检查特定值。 For example: 例如:
public bool IsDevicePresent()
{
byte[] buffer = new byte[3];
int count = 0;
try
{
port.Write(new byte[] { 0x5A, 0x01 }, 0, 2);
count = port.Read(buffer, 0, 3);
}
catch (TimeoutException)
{
return false;
}
return (buffer[0] == 0x07 && count == 3);
}
port
is an instance of ISerialPort
. port
是ISerialPort
的实例。
I'm trying to write some tests for the IsDevicePresent
function, using Moq to mock an ISerialPort
. 我正在尝试使用Moq模拟ISerialPort
来为IsDevicePresent
函数编写一些测试。 However, I can't figure out how to get Moq to set values in the passed byte array ( buffer
). 但是,我无法弄清楚如何让Moq在传递的字节数组( buffer
)中设置值。 I can get Moq to return 3, but how can I get Moq to set the first byte in buffer
to 0x07? 我可以让Moq返回3,但是如何让Moq将buffer
的第一个字节设置为0x07?
var mock = new Mock<ISerialPort>();
mock.Setup(m => m.Read(It.IsAny<byte[]>(), It.IsAny<int>(),It.IsAny<int>()))
.Returns(3);
You can use the Callback
method to access the incoming parameters and set first element of the passed in buffer: 您可以使用Callback
方法访问传入的参数并设置传入缓冲区的第一个元素:
mock.Setup(m => m.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns(3)
.Callback<byte[], int, int>((buffer, offset, count) => { buffer[0] = 0x07; });
You can do the same thing inside the Returns
您可以在Returns
执行相同的操作
mock.Setup(m => m.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
.Returns<byte[], int, int>((buffer, offset, count) =>
{
buffer[0] = 0x07;
return 3;
});
But using the Callback
is more easier to follow than making the side-effect inside the Returns
但是,使用Callback
比在Returns
产生副作用要容易得多
To follow up on @nemesv's answer, the compiler didn't like the generic parameters in the Callback method for me, so I had to use the types in the lambda. 要跟踪@nemesv的答案,编译器对我而言不喜欢Callback方法中的通用参数,因此我不得不使用lambda中的类型。
mock.Setup(m => m.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
.Callback((byte[] buffer, int offset, int count) => {}).Returns(3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.