C# 模拟委托抛出转换类型异常

发布于 2025-01-13 04:54:48 字数 2253 浏览 1 评论 0原文

我有一个委托,它指向没有参数的方法,它返回对象数据列表的列表。 我使用 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);

enter code here

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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南汐寒笙箫 2025-01-20 04:54:48

要模拟委托,语法是:

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);
}

To mock a delegate, the syntax is :

appTenantListHandlerMoq.Setup(_ => _()).Returns(Task.FromResult(tenantsMoq));

But in your case, this will do noting, but the instance sut don't call the mocked delegate. You need to modify MyClass to inject the mocked delegate like :

public class MyClass 
{
    public delegate Task<IEnumerable<AppTenant>> AppTenantListHandler();
    
    AppTenantListHandler appTenantListHandler;


    public MyClass ()
        : this(new AppTenantListHandler(GetTenant))
    { }

    public MyClass (AppTenantListHandler appTenantListHandler )
    {
        this.appTenantListHandler = appTenantListHandler ;
    }
    ...
}

Then in the test :

[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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文