在子实例设置属性值设置为一个固定值与Autofixture [英] Setting property value on child instance to a fixed value with Autofixture

查看:131
本文介绍了在子实例设置属性值设置为一个固定值与Autofixture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能建立一个与家长时Autofixture对孩子实例分配一个固定值的属性?这将添加默认值到像一个魅力子实例的所有属性,但我想重写,并分配一个特定的值到子实例的属性之一。

Is it possible to assign a fixed value to a property on a child instance when building a parent with Autofixture? It will add default values to all the properties on the child instance like a charm, but I would like to override and assign a specific value to one of the properties on the child instance.

鉴于这种父/子关系:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Address Address { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public int Number { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
}



我想一个特定的值分配给市物业上的地址实例。我想在这个测试代码行:

I would like to assign a specific value to the City property on the address instance. I was thinking in the lines of this test code:

var fixture = new Fixture();

var expectedCity = "foo";

var person = fixture
    .Build<Person>()
    .With(x => x.Address.City, expectedCity)
    .Create();

Assert.AreEqual(expectedCity, person.Address.City);

这是不可能的。我想,通过反射异常

That is not possible. I guess, by the reflection exception

System.Reflection.TargetException : Object does not match target type.



...这Autofixture试图值分配给在Person实例城酒店,而不是一个地址实例。

...that Autofixture tries to assign the value to a City property on the Person instance instead of an Address instance.

有什么建议?

是的,我知道我可以只添加像一个额外的步骤以下内容:

And yes, I know that I could just add an extra step like the following:

var fixture = new Fixture();

var expectedCity = "foo";

// extra step begin
var address = fixture
    .Build<Address>()
    .With(x => x.City, expectedCity)
    .Create();
// extra step end

var person = fixture
    .Build<Person>()
    .With(x => x.Address, address)
    .Create();

Assert.AreEqual(expectedCity, person.Address.City);



...但所期待的第一个版本或类似的东西(更少的步骤,更简洁)。

...but was hoping for the first version or something similar (fewer steps, more terse).

请注意:我使用Autofixture v3.22.0

Note: I'm using Autofixture v3.22.0

推荐答案

为了不被轻视的问题,但最简单的解决方案,实际上可能是这样的:

Not to be dismissive of the question, but the simplest solution might actually be this:

[Fact]
public void SimplestThingThatCouldPossiblyWork()
{
    var fixture = new Fixture();
    var expectedCity = "foo";
    var person = fixture.Create<Person>();
    person.Address.City = expectedCity;
    Assert.Equal(expectedCity, person.Address.City);
}



分配到物业是大多数语言已经处于(C#擅长肯定一样),所以我不认为AutoFixture需要一个复杂的DSL重现该功能一半。

Assignment of explicit values to properties is something most languages already excel at (C# certainly does), so I don't think AutoFixture needs a complicated DSL to reproduce half of that functionality.

这篇关于在子实例设置属性值设置为一个固定值与Autofixture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆