使用OmitAutoProperties进行AutoFixture定制不起作用

时间:2019-04-10 14:13:05

标签: c# unit-testing nunit autofixture

我正在将AutoFixture与NUnit一起使用,当我使用OmitAutoProperties时,仍会自动填充属性。这是我的自定义外观:

    public class ContractItemCustomization : ICustomization
    {
        public void Customize(IFixture fixture)
        {
            var orderReceivedContractItem = fixture.Build<ContractItem>()
                                    .OmitAutoProperties()
                                    .With(x => x.Status, new ContractStatus
                                    {
                                        ContractLifecycleStatus = ContractLifecycleStatus.ORDER_RECEIVED
                                    });

            fixture.Customizations.Add(orderReceivedContractItem);
        }
    }

这是我使用自定义方式的方法:

        [SetUp]
        public void Setup()
        {
            fixture = new Fixture().Customize(
                new CompositeCustomization(
                    new ContractItemCustomization()));


            contractItem = fixture.Create<ContractItem>();
        }

我想念什么?

1 个答案:

答案 0 :(得分:0)

好吧,在更深入地了解AutoFixture文档之后,我找到了解决方案:

代替:

fixture.Customizations.Add(orderReceivedContractItem);

我必须这样做:

fixture.Register(() => orderReceivedContractItem.Create());