C# 模拟委托抛出转换类型异常
我有一个委托,它指向没有参数的方法,它返回对象数据列表的列表。 我使用 nUnit、fixture 和 mock 进行测试。我已经创建了记录列表,我喜欢通过委托模拟我的静态方法,但我收到错误
appTenantListHandlerMoq.Setup(_ => _).Returns(tenantsMoq);
MyClass
public class MyClass
{
public delegate Task<IEnumerable<AppTenant>> AppTenantListHandler();
AppTenantListHandler appTenantListHandler;
public MyClass ()
{
appTenantListHandler = new AppTenantListHandler(GetTenant);
}
public async Task Run(){
var tenantList = await appTenantListHandler();
}
public static async Task<IEnumerable<AppTenant>> GetTenant()
{
List<AppTenant> dataList = new List<AppTenant>();
return dataList;
}
}
测试类
[TestFixture]
public class MyClassTests
{
private readonly MyClass sut;
private Mock<MyClass> MyClassMoq;
private readonly Mock<ILogger> loggerMoq;
private readonly Mock<ITelemetryInitializer> telemetryInitializerMoq;
public MyClassTests()
{
this.loggerMoq = new Mock<ILogger>();
this.telemetryInitializerMoq = new Mock<ITelemetryInitializer>();
this.MyClassMoq = new Mock<MyClass>();
this.sut = new MyClass(loggerMoq.Object);
}
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
//Arrange
var fixture = new Fixture();
var tenantsMoq = fixture.CreateMany<AppTenant>(5);
var appTenantListHandlerMoq = new Mock<MyClass.AppTenantListHandler>();
appTenantListHandlerMoq.Setup(_ => _).Returns(tenantsMoq);
//Act
var actualResult = sut.RunAsync(this.telemetryInitializerMoq.Object);
//Assert
Assert.NotNull(actualResult);
}
}
Error2 < /代码>
<一href="https://i.sstatic.net/mB3PT.png" rel="nofollow noreferrer">
I have delegate which pointing to method with no parameter and it return list of object data List.
I am using nUnit, fixture and mock for the testing. I have created list of records that I like my static method to mock with via delegate but I am getting error
appTenantListHandlerMoq.Setup(_ => _).Returns(tenantsMoq);
MyClass
public class MyClass
{
public delegate Task<IEnumerable<AppTenant>> AppTenantListHandler();
AppTenantListHandler appTenantListHandler;
public MyClass ()
{
appTenantListHandler = new AppTenantListHandler(GetTenant);
}
public async Task Run(){
var tenantList = await appTenantListHandler();
}
public static async Task<IEnumerable<AppTenant>> GetTenant()
{
List<AppTenant> dataList = new List<AppTenant>();
return dataList;
}
}
Test Class
[TestFixture]
public class MyClassTests
{
private readonly MyClass sut;
private Mock<MyClass> MyClassMoq;
private readonly Mock<ILogger> loggerMoq;
private readonly Mock<ITelemetryInitializer> telemetryInitializerMoq;
public MyClassTests()
{
this.loggerMoq = new Mock<ILogger>();
this.telemetryInitializerMoq = new Mock<ITelemetryInitializer>();
this.MyClassMoq = new Mock<MyClass>();
this.sut = new MyClass(loggerMoq.Object);
}
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
//Arrange
var fixture = new Fixture();
var tenantsMoq = fixture.CreateMany<AppTenant>(5);
var appTenantListHandlerMoq = new Mock<MyClass.AppTenantListHandler>();
appTenantListHandlerMoq.Setup(_ => _).Returns(tenantsMoq);
//Act
var actualResult = sut.RunAsync(this.telemetryInitializerMoq.Object);
//Assert
Assert.NotNull(actualResult);
}
}
Error2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要模拟委托,语法是:
但在您的情况下,这将引起注意,但实例
sut
不会调用模拟委托。您需要修改MyClass
以注入模拟委托,如下所示:然后在测试中:
To mock a delegate, the syntax is :
But in your case, this will do noting, but the instance
sut
don't call the mocked delegate. You need to modifyMyClass
to inject the mocked delegate like :Then in the test :