C# Mock Delegate 抛出类型转换异常
C# Mock Delegate throw exception of type Conversion
我有一个委托,它指向没有参数的方法,它 return 对象数据列表列表。
我正在使用 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;
}
}
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
要模拟委托,语法是:
appTenantListHandlerMoq.Setup(_ => _()).Returns(Task.FromResult(tenantsMoq));
但在您的情况下,这会引起注意,但实例 sut
不会调用模拟委托。您需要修改 MyClass
以注入模拟委托,例如:
public class MyClass
{
public delegate Task<IEnumerable<AppTenant>> AppTenantListHandler();
AppTenantListHandler appTenantListHandler;
public MyClass ()
: this(new AppTenantListHandler(GetTenant))
{ }
public MyClass (AppTenantListHandler appTenantListHandler )
{
this.appTenantListHandler = appTenantListHandler ;
}
...
}
然后在测试中:
[Test]
public void Test1()
{
//Arrange
var fixture = new Fixture();
var tenantsMoq = fixture.CreateMany<AppTenant>(5);
var appTenantListHandlerMoq = new Mock<MyClass.AppTenantListHandler>();
appTenantListHandlerMoq.Setup(_ => _()).Returns(Task.FromResult(tenantsMoq));
MyClass sut = new MyClass(appTenantListHandlerMoq);
//Act
var actualResult = sut.RunAsync(this.telemetryInitializerMoq.Object);
//Assert
Assert.NotNull(actualResult);
}
我有一个委托,它指向没有参数的方法,它 return 对象数据列表列表。 我正在使用 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;
}
}
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
要模拟委托,语法是:
appTenantListHandlerMoq.Setup(_ => _()).Returns(Task.FromResult(tenantsMoq));
但在您的情况下,这会引起注意,但实例 sut
不会调用模拟委托。您需要修改 MyClass
以注入模拟委托,例如:
public class MyClass
{
public delegate Task<IEnumerable<AppTenant>> AppTenantListHandler();
AppTenantListHandler appTenantListHandler;
public MyClass ()
: this(new AppTenantListHandler(GetTenant))
{ }
public MyClass (AppTenantListHandler appTenantListHandler )
{
this.appTenantListHandler = appTenantListHandler ;
}
...
}
然后在测试中:
[Test]
public void Test1()
{
//Arrange
var fixture = new Fixture();
var tenantsMoq = fixture.CreateMany<AppTenant>(5);
var appTenantListHandlerMoq = new Mock<MyClass.AppTenantListHandler>();
appTenantListHandlerMoq.Setup(_ => _()).Returns(Task.FromResult(tenantsMoq));
MyClass sut = new MyClass(appTenantListHandlerMoq);
//Act
var actualResult = sut.RunAsync(this.telemetryInitializerMoq.Object);
//Assert
Assert.NotNull(actualResult);
}