如何 mock/stub/shim SerialPort.GetPortNames()
How to mock/stub/shim SerialPort.GetPortNames()
我正在尝试在 C# 中测试 ApiController class,特别是使用 SerialPort.GetPortNames() 的函数。这个 returns 取决于它 运行 所在的机器,所以我希望能够 Shim/stub/mock 它以某种方式拥有它 return 虚拟数据。
使用 visual studio 2015,项目目标 .net 4.5.2,并使用 Microsoft.VisualStudio.TestTools.UnitTesting
我认为 Microsoft Fakes 能够完全满足我的需要,但我没有 Visual Studio 企业版。
我了解到 Moq 在这里毫无价值,并且 pose 不适用于该项目所针对的 .Net 版本 (4.5.2)。
我研究过 prig,但除了 datetime.now() 之外,我不知道如何配置它。
而且我不明白如何使用 Smock 进行实际测试。
[HttpGet]
[Route("PortList")]
public HttpResponseMessage SerialPortList()
{
HttpResponseMessage response;
try
{
List<string> Ports = new List<string>(SerialPort.GetPortNames());
response = Request.CreateResponse(HttpStatusCode.OK, Ports);
}
catch (Exception ex)
{
response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
return response;
}
我希望能够从 SerialPort 填充(正确的词?)静态方法,并让它 return 串口的虚拟列表(["COM1","COM2" ]).
我绕过 SerialPort.GetPortNames
之类的模拟静态方法的方法是添加一个间接层。在您的情况下,最简单的方法是创建一个 SerialPortList
重载,它接受 Func<string[]>
像这样。
public HttpResponseMessage SerialPortList()
{
return SerialPortList(SerialPort.GetPortNames);
}
public HttpResponseMessage SerialPortList(Func<string[]> getPortNames)
{
HttpResponseMessage response;
try
{
List<string> Ports = new List<string>(getPortNames());
response = Request.CreateResponse(HttpStatusCode.OK, Ports);
}
catch (Exception ex)
{
response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
return response;
}
在你的单元测试中...
public void Test()
{
var portNames = new[] { "COM1" };
foo.SerialPortList(() => portNames);
}
我正在尝试在 C# 中测试 ApiController class,特别是使用 SerialPort.GetPortNames() 的函数。这个 returns 取决于它 运行 所在的机器,所以我希望能够 Shim/stub/mock 它以某种方式拥有它 return 虚拟数据。
使用 visual studio 2015,项目目标 .net 4.5.2,并使用 Microsoft.VisualStudio.TestTools.UnitTesting
我认为 Microsoft Fakes 能够完全满足我的需要,但我没有 Visual Studio 企业版。
我了解到 Moq 在这里毫无价值,并且 pose 不适用于该项目所针对的 .Net 版本 (4.5.2)。
我研究过 prig,但除了 datetime.now() 之外,我不知道如何配置它。
而且我不明白如何使用 Smock 进行实际测试。
[HttpGet]
[Route("PortList")]
public HttpResponseMessage SerialPortList()
{
HttpResponseMessage response;
try
{
List<string> Ports = new List<string>(SerialPort.GetPortNames());
response = Request.CreateResponse(HttpStatusCode.OK, Ports);
}
catch (Exception ex)
{
response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
return response;
}
我希望能够从 SerialPort 填充(正确的词?)静态方法,并让它 return 串口的虚拟列表(["COM1","COM2" ]).
我绕过 SerialPort.GetPortNames
之类的模拟静态方法的方法是添加一个间接层。在您的情况下,最简单的方法是创建一个 SerialPortList
重载,它接受 Func<string[]>
像这样。
public HttpResponseMessage SerialPortList()
{
return SerialPortList(SerialPort.GetPortNames);
}
public HttpResponseMessage SerialPortList(Func<string[]> getPortNames)
{
HttpResponseMessage response;
try
{
List<string> Ports = new List<string>(getPortNames());
response = Request.CreateResponse(HttpStatusCode.OK, Ports);
}
catch (Exception ex)
{
response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
return response;
}
在你的单元测试中...
public void Test()
{
var portNames = new[] { "COM1" };
foo.SerialPortList(() => portNames);
}