我正在为IotEdge自定义模块编写xunit测试,在这里我需要模拟ModuleClient.CreateFromEnvironmentAsync(),这将打开到边缘运行时的连接。
物联网边缘模块代码如下所示:
var amqpSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
ITransportSettings[] settings = { amqpSetting };
// Open a connection to the Edge runtime
this.ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
await this.ioTHubModuleClient.OpenAsync();单元测试代码如下所示:
var amqpSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
ITransportSettings[] settings = { amqpSetting };
var moduleClientMoq = new Mock<ModuleClient>(ModuleClient.CreateFromEnvironmentAsync(settings)); // getting an exception-"System.NotSupportedException: 'Type to mock must be an interface or an abstract or non-sealed class." 我得到了"System.NotSupported“exception.Please建议如何模拟模块客户端。
发布于 2019-12-11 20:52:08
正如注释和GitHub问题中所建议的那样,您应该围绕ModuleClient实现一个包装器,并将您的测试建立在这个包装器上。正如GitHub问题中所述,ModuleClient背后的团队不会在短期内实现接口(“只有您拥有的模拟类型”),您还可以获得其他好处:
https://stackoverflow.com/questions/57986505
复制相似问题