在此示例代码中,我想配置一个 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;
}
问题是,每当我的工厂返回 null
时,我都会得到一个 InvalidOperationException
:
The specimen returned by the decorated ISpecimenBuilder is not compatible with System.String.
对于我在工厂内返回 null
的任何类型,都会发生这种情况。有什么方法可以配置 AutoFixture
为请求的对象返回 null
?
最佳答案
这是一个缺陷。它应该在 AutoFixture 3.36.11 中解决.
关于c# - 如何自定义 AutoFixture 有时会返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33619619/