【问题标题】:Is there a way to populate an object instance using AutoFixture?有没有办法使用 AutoFixture 填充对象实例?
【发布时间】:2019-03-28 00:34:00
【问题描述】:

这是我正在尝试做的一个示例

var foo = CreateFoo(); // I don't have access to the internals of CreateFoo

fixture.Populate(foo); // foo should now have random property values

显然,没有Fixture.Populate 方法。但是我应该如何解决这个问题? 谢谢!

【问题讨论】:

  • 您可以使用 AutoFixture 自定义,这个非常流行,当您要求 autofixture 创建一个对象时,它会使用随机值填充属性...通常 NameOfProperty 附加一个 GUID.. 使用它,只需按如下方式创建您的 IFixture: IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());

标签: autofixture


【解决方案1】:

AutoFixture 确实不提供 API 来填充现有对象的可写属性。但是,您可以使用以下 sn-p 来实现所需的行为:

var fixture = new Fixture();
var objectToFill = new TypeWithProperties();

new AutoPropertiesCommand().Execute(objectToFill, new SpecimenContext(fixture));

Assert.NotNull(objectToFill.Value);

如果您经常需要,您可以在您的测试项目中为IFixture 创建一个扩展方法。

【讨论】:

    猜你喜欢
    • 2019-08-31
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2021-03-23
    相关资源
    最近更新 更多