AutoFixture的Without()方法在只有一个构造函数且所有参数都需要的情况下无法工作?
创始人
2024-11-12 13:30:34
0

此问题可以通过显式指定需要排除的参数为 nullDefaultValue”来解决。例如,假设有以下类:

public class MyClass
{
    public int MyInt { get; }
    public string MyString { get; }
    public bool MyBool { get; }
    
    public MyClass(int myInt, string myString, bool myBool)
    {
        MyInt = myInt;
        MyString = myString;
        MyBool = myBool;
    }
}

如果我们只想要自动填充 myIntmyString 这两个构造函数参数,并允许 myBool 接受默认值,代码如下:

var fixture = new Fixture();
var myClass = fixture.Build()
                    .Without(x => x.MyBool)
                    .Create();

如果我们希望将 myBool 参数设为 false,并且仍然使用 Without() 方法排除该参数,代码如下:

var fixture = new Fixture();
var myClass = fixture.Build()
                    .Without(x => x.MyBool)
                    .With(x => x.MyBool, false)
                    .Create();

这样,我们就可以在只有一个构造函数的情况下排除某些参数或将它们设为特定值。

相关内容

热门资讯

透视讲解!wpk私人局有透视吗... 您好,wepoker透视辅助下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
透视app!红龙poker有辅... 透视app!红龙poker有辅助吗(透视)开挂辅助器(果然存在有挂)-哔哩哔哩所有人都在同一条线上,...
透视辅助!epoker透视底牌... 透视辅助!epoker透视底牌,哈糖大菠萝开挂,阶段教程(总是是有挂)-哔哩哔哩1、玩家可以在哈糖大...
透视插件!wepoker辅助器... 透视插件!wepoker辅助器激活码(透视)开挂辅助器(其实有挂)-哔哩哔哩1)wepoker辅助器...
透视教程!hhpoker德州挂... 透视教程!hhpoker德州挂真的有吗,werplan透视挂,学习教程(一贯存在有挂)-哔哩哔哩1、...
透视游戏!aapoker免费透... 透视游戏!aapoker免费透视脚本,xpoker辅助工具,诀窍教程(一贯存在有挂)-哔哩哔哩1)a...
透视大厅!aa poker透视... 透视大厅!aa poker透视软件(透视)开挂辅助器(原来是有挂)-哔哩哔哩1、超多福利:超高返利,...
透视脚本!xpoker辅助工具... 透视脚本!xpoker辅助工具,pokernow辅助工具,方案教程(一贯真的有挂)-哔哩哔哩1、不需...
透视修改器!wepoker透视... 透视修改器!wepoker透视有吗(透视)开挂辅助神器(竟然是有挂)-哔哩哔哩wepoker透视有吗...
透视科技!wejoker透视方... 透视科技!wejoker透视方法,wepoker线上大神,操作教程(确实存在有挂)-哔哩哔哩在进入w...