自定义特定类型会干扰其他类型的共享基本属性的填充

时间:2016-02-18 16:36:28

标签: autofixture

我正在尝试使用AutoFixture来简化自动化测试中的数据实体数量。实体具有基本id和几种用于确定相等性以及对象是否已被持久化的方法。有些实体需要特定的值,所以我尝试自定义它们,但之后开始尝试保存其他实体时出错,因为基础ID没有被填充。

这是一个简化;第三次测试失败并说明了问题。我是否误解了Customize如何工作,或者这是AutoFixture中的错误?我使用的是版本3.40。

[TestFixture]
public class AutoFixtureTestsWithWorkaround
{
    public class Entity
    {
        public string Id { get; set; }
    }

    public class ContractType : Entity
    {
        public string Description { get; set; }
    }

    public class Customer : Entity
    {
        public string Name { get; set; }
    }

    [Test]
    public void Can_populate_ContractType_with_customization_using_specimen_context()
    {
        var fixture = new Fixture();
        fixture.Customize<ContractType>(composer => composer
            .With(x => x.Id, "FM")
            .With(x => x.Description, "Full Maintenance")
            );

        var specimenContext = new SpecimenContext(fixture);
        var ct = (ContractType)specimenContext.Resolve(typeof(ContractType));

        Assert.AreEqual("FM", ct.Id);
        Assert.AreEqual("Full Maintenance", ct.Description);
    }

    [Test]
    public void Can_populate_customer_id()
    {
        var fixture = new Fixture();
        var specimenContext = new SpecimenContext(fixture);
        var customer = (Customer)specimenContext.Resolve(typeof(Customer));
        Assert.NotNull(customer.Id);
    }

    [Test]
    public void Can_populate_customer_id_with_ContractType_customization_using_with()
    {
        var fixture = new Fixture();
        fixture.Customize<ContractType>(composer => composer
            .With(x => x.Id, "FM")
            .With(x => x.Description, "Full Maintenance")
            );

        var specimenContext = new SpecimenContext(fixture);
        var customer = (Customer)specimenContext.Resolve(typeof(Customer));

        Assert.NotNull(customer.Id); // This test fails
    }

    [Test]
    public void Can_populate_customer_id_with_ContractType_customization_using_do()
    {
        var fixture = new Fixture();
        fixture.Customize<ContractType>(composer => composer
            .Do(x => x.Id = "FM")
            .With(x => x.Description, "Full Maintenance")
            );

        var specimenContext = new SpecimenContext(fixture);
        var customer = (Customer)specimenContext.Resolve(typeof(Customer));
        Assert.NotNull(customer.Id);
    }
}

0 个答案:

没有答案