Nat*_*n A 3 c# unit-testing autofixture
在此示例代码中,我想配置一个Fixture
对象以null
在一半的时间内返回字符串。
void Test()
{
var fixture = new Fixture();
fixture.Customize<string>(x => x.FromFactory(CreateString));
var str1 = fixture.Create<string>();
//error occurs here when string should come back null
var str2 = fixture.Create<string>();
}
bool _createString = false;
string CreateString()
{
_createString = !_createString;
return _createString ? "test" : null;
}
Run Code Online (Sandbox Code Playgroud)
问题是,每当我的工厂返回时null
,我都会得到InvalidOperationException
:
装饰的 ISpecimenBuilder 返回的样本与 System.String 不兼容。
对于我null
在工厂内返回的任何类型,都会发生这种情况。有没有办法配置AutoFixture
返回null
请求的对象?