问题描述
我用NSubstitute了很多。我喜欢它。
我只是在寻找到AutoFixture。这似乎太好了!
我看到 AutoFixture的NSubstitute 并看到起订量的几个例子就如何使用此功能。
不过,我似乎无法把它翻译成NSubstitute。
我尝试这样做:
VAR夹具=新灯()自定义(新AutoNSubstituteCustomization())。
变种addDest = Substitute.For&其中; IPerson>();
使用:
公共接口IPersonEntity
{
INT ID {获得;组; }
字符串名字{获得;组;}
字符串名字{获得;组;}
日期时间DATEOFBIRTH {获得;组; }
焦炭性别{获得;组; }
}
和我得到一个对象,但没有属性的填充(一种AutoFixture点的)。
我也试过:
VAR夹具=新灯()自定义(新AutoNSubstituteCustomization())。
VAR的结果= fixture.Create< IPersonEntity>();
这也给了我一个对象没有填充性能。 (请注意,如果我做了上述与 PersonEntity
类,则属性都填充。)
我相信,有一种方法,使这项工作,但我似乎无法找到它。
因此, 给我的 IPersonEntity
接口上面,没有人知道如何使用AutoFixture和NSubstitute给我一个人口稠密IPersonEntity对象? P>
而不是定制的灯具
实例的 AutoNSubstituteCustomization
您可以使用下面的定制:
VAR夹具=新灯()。自定义(
新AutoPopulatedNSubstitutePropertiesCustomization());
VAR的结果= fixture.Create< IPersonEntity>();
// - >所有属性现在应该填充。
在 AutoPopulatedNSubstitutePropertiesCustomization
定义为:
内部类AutoPopulatedNSubstitutePropertiesCustomization
:ICustomization
{
公共无效自定义(IFixture夹具)
{
fixture.ResidueCollectors.Add(
新的后处理(
新NSubstituteBuilder(
新MethodInvoker(
新NSubstituteMethodQuery())),
新AutoPropertiesCommand(
新PropertiesOnlySpecification())));
}
私有类PropertiesOnlySpecification:IRequestSpecification
{
公共BOOL IsSatisfiedBy(对象请求)
{
返回请求的PropertyInfo;
}
}
}
所不同的 AutoNSubstituteCustomization
是上面的定制也是装饰与后处理
实例来自动设置为请求类型的所有公共属性的值。
引用
上面的解决方案是由以下博客文章马克塞曼的启发:
I use NSubstitute a lot. And I love it.
I am just looking into AutoFixture. It seems great!
I have seen AutoFixture for NSubstitute and seen a few examples in Moq on how to use this feature.
But I can't seem to translate it into NSubstitute.
I tried this:
var fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
var addDest = Substitute.For<IPerson>();
Using:
public interface IPersonEntity
{
int ID { get; set; }
string FirstName { get; set;}
string LastName { get; set;}
DateTime DateOfBirth { get; set; }
char Gender { get; set; }
}
And I get an object, but none of the properties are populated (kind of the point of AutoFixture).
I also tried:
var fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
var result = fixture.Create<IPersonEntity>();
That also gave me an object with no populated properties. (Note if I do the above with a PersonEntity
class, then the properties are all populated.)
I am sure that there is a way to make this work, but I can't seem to find it.
So, given my IPersonEntity
interface above, does anyone know how to use AutoFixture and NSubstitute to give me a populated IPersonEntity object?
Instead of customizing the Fixture
instance with the AutoNSubstituteCustomization
you may use the customization below:
var fixture = new Fixture().Customize(
new AutoPopulatedNSubstitutePropertiesCustomization());
var result = fixture.Create<IPersonEntity>();
// -> All properties should be populated now.
The AutoPopulatedNSubstitutePropertiesCustomization
is defined as:
internal class AutoPopulatedNSubstitutePropertiesCustomization
: ICustomization
{
public void Customize(IFixture fixture)
{
fixture.ResidueCollectors.Add(
new Postprocessor(
new NSubstituteBuilder(
new MethodInvoker(
new NSubstituteMethodQuery())),
new AutoPropertiesCommand(
new PropertiesOnlySpecification())));
}
private class PropertiesOnlySpecification : IRequestSpecification
{
public bool IsSatisfiedBy(object request)
{
return request is PropertyInfo;
}
}
}
The difference with the AutoNSubstituteCustomization
is that the above customization is also decorated with a Postprocessor
instance to automatically set values for all the public properties of the requested type.
References:
The above solution is inspired by the following blog articles by Mark Seemann:
- How to configure AutoMoq to set up all properties
- How to automatically populate properties with AutoMoq
这篇关于如何示例使用AutoFixture与NSubstitute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!