AutoFixture.Freeze IEnumerable of Mocks 可以翻译为“冻结 Mock 对象的 IEnumerable”。
创始人
2024-11-12 13:30:22
0

解决方法:

使用AutoFixture.Freeze方法可以冻结Mock对象的IEnumerable。下面是一个示例代码:

using AutoFixture;
using Moq;
using System.Collections.Generic;

public class MyClass
{
    private IEnumerable myInterfaceList;

    public MyClass(IEnumerable myInterfaceList)
    {
        this.myInterfaceList = myInterfaceList;
    }

    public void DoSomething()
    {
        foreach (var myInterface in myInterfaceList)
        {
            myInterface.DoSomething();
        }
    }
}

public interface IMyInterface
{
    void DoSomething();
}

public class MyTestClass
{
    public void TestMethod()
    {
        var fixture = new Fixture();
        var mockInterface = fixture.Freeze>();
        var myClass = fixture.Create();

        myClass.DoSomething();

        mockInterface.Verify(x => x.DoSomething(), Times.Once);
    }
}

在上面的代码中,我们使用AutoFixture创建了一个Fixture对象。然后我们使用Fixture的Freeze方法将IMyInterface接口的Mock对象冻结,以便在测试中可以访问它。

然后,我们使用Fixture的Create方法创建了一个MyClass对象,自动填充了其构造函数参数。在这种情况下,我们的构造函数需要一个IEnumerable参数,所以Fixture会自动填充一个IEnumerable对象。

最后,我们调用了myClass的DoSomething方法,并使用Mock对象的Verify方法验证了DoSomething方法被调用了一次。

这样,我们就可以在测试中使用冻结的Mock对象的IEnumerable。

相关内容

热门资讯

透视能赢!aapoker脚本(... 透视能赢!aapoker脚本(透视)透视软件(其实是有挂)1、每一步都需要思考,不同水平的挑战会更加...
透视脚本!newpoker脚本... 透视脚本!newpoker脚本,约局吧德州可以透视吗(确实是有挂);1、很好的工具软件,可以解锁游戏...
透视科技!aapoker俱乐部... 透视科技!aapoker俱乐部靠谱吗(透视)脚本(果然真的是有挂);aapoker俱乐部靠谱吗辅助器...
透视教程!xpoker辅助神器... 透视教程!xpoker辅助神器,pokemmo辅助器(确实存在有挂);1、xpoker辅助神器透视辅...
透视规律!aapoker辅助器... 透视规律!aapoker辅助器怎么用(透视)透视脚本入口(本来真的有挂)1、aapoker辅助器怎么...
透视规律!云扑克有透视吗,哈糖... 透视规律!云扑克有透视吗,哈糖大菠萝攻略(原来真的有挂)1、哈糖大菠萝攻略系统规律教程、哈糖大菠萝攻...
透视ai!aapoker免费透... 透视ai!aapoker免费透视脚本(透视)透视脚本(竟然是有挂);1、任何aapoker免费透视脚...
透视最新!werplan透视挂... 透视最新!werplan透视挂,cloudpoker作弊(一直真的是有挂)1、任何cloudpoke...
透视好牌!佛手在线大菠萝为什么... 透视好牌!佛手在线大菠萝为什么都输,福建大菠萝万能辅助器(真是存在有挂);1)佛手在线大菠萝为什么都...
透视讲解!aapoker透视方... 透视讲解!aapoker透视方法(透视)透视方法(原来真的有挂);暗藏猫腻,小编详细说明aapoke...