有时如何将 AutoFixture 自定义为 Return Null
How to Customize AutoFixture to Return Null Sometimes
在此示例代码中,我想将一个 Fixture
对象配置为 return 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;
}
问题是,每当我的工厂 returns null
,我得到一个 InvalidOperationException
:
The specimen returned by the decorated ISpecimenBuilder is not compatible with System.String.
我在工厂内 return null
的任何类型都会发生这种情况。有没有办法为请求的对象配置 AutoFixture
到 return null
?
这是一个缺陷。它应该在 AutoFixture 3.36.11.
中解决
在此示例代码中,我想将一个 Fixture
对象配置为 return 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;
}
问题是,每当我的工厂 returns null
,我得到一个 InvalidOperationException
:
The specimen returned by the decorated ISpecimenBuilder is not compatible with System.String.
我在工厂内 return null
的任何类型都会发生这种情况。有没有办法为请求的对象配置 AutoFixture
到 return null
?
这是一个缺陷。它应该在 AutoFixture 3.36.11.
中解决