简体   繁体   English

如何为某些属性生成具有固定值的实例?

[英]How to generate instance with fixed value for certain property?

We use AutoFixture for test data generation, one of the scenarios requires us to provide fixed data, so we can test the method below.我们使用 AutoFixture 进行测试数据生成,其中一种场景需要我们提供固定数据,所以我们可以测试下面的方法。

public enum OrderState { Initial = 0, State1 = 1, State2 = 2, State3 = 3 };

public record Order(Guid Id, string Reference, OrderState State);   

public static class Filter
{
  public static IEnumerable<Order> ByState(IEnumerable<Order> orders, OrderState state)
  {
    return orders.Where(order => order.State == state);
  }
}

Test测试

[Fact]
public void Returns_only_orders_with_initial_state()
{
  // Arrange
  var fixture = new Fixture();
  var order1 = fixture.Build(o => o.State, OrderState.Initial).Create();
  var allOrders = new[] 
  {
    fixture.Build(o => o.State, OrderState.State1).Create(),
    fixture.Build(o => o.State, OrderState.State2).Create(),
    fixture.Build(o => o.State, OrderState.State3).Create(),
    order1
  };
   
  // Act
  var actual = new Filter().ByState(allOrders, OrderState.Initial);

  // Assert
  actual.Should().BeEquivalentTo(new[] { order1 });
}

Test fails because AutoFixture can not write to readonly properties - I understand that.测试失败,因为 AutoFixture 无法写入只读属性 - 我明白这一点。

How can I create an instance with fixed value for the State property, but still use "random" values for other properties?如何为State属性创建具有固定值的实例,但仍对其他属性使用“随机”值?

You can achieve this using AutoFixture's SpecimenBuilder facility.您可以使用 AutoFixture 的SpecimenBuilder工具来实现这一点。

An example of his can be found in this post他的一个例子可以在这篇文章中找到

A demonstration for your use case:您的用例的演示:

using AutoFixture.Kernel;
using System.Reflection;


namespace Example
{
    public enum OrderState { Initial = 0, State1 = 1, State2 = 2, State3 = 3 };

    public record Order(Guid Id, string Reference, OrderState State);

    public class OrderStateArg : ISpecimenBuilder
    {
        private readonly OrderState _state;

        public OrderStateArg(OrderState state)
        {
            _state = state;
        }

        public object Create(object request, ISpecimenContext context)
        {
            var pi = request as ParameterInfo;
        
            if (pi == null)
            {
                return new NoSpecimen();
            }

            if (pi.Member.DeclaringType != typeof(Order) ||
                pi.ParameterType != typeof(OrderState) ||
                pi.Name != "State")
            {
                return new NoSpecimen();
            }

            return _state;
        }
    }

    public static class Filter
    {
        public static IEnumerable<Order> ByState(IEnumerable<Order> orders, OrderState state)
        {
            return orders.Where(order => order.State == state);
        }
    }

    public class Class1
    {
        [Fact]
        public void Returns_only_orders_with_initial_state()
        {
            var fixture = new Fixture();
            fixture.Customizations.Add(new OrderStateArg(OrderState.Initial));

            var order1 = fixture.Create<Order>();

            fixture.Customizations.Clear();

            var allOrders = new Order[] 
            {
                fixture.Create<Order>(),
                fixture.Create<Order>(),
                fixture.Create<Order>(),
                order1
            };

            // Act
            // Note on your original code, Filter is a static class
            // so calling its methods do not require the new keyword.
            var actual = Filter.ByState(allOrders, OrderState.Initial);

            // Assert
            actual.Should().BeEquivalentTo(new[] { order1 });
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Autofixture将子实例上的属性值设置为固定值 - Setting property value on child instance to a fixed value with Autofixture 如何根据字符串的值设置实例的属性? - how to set a property of an instance based on the value of a string? 如何每次都在一定范围内每次都生成固定的“伪随机”双打列表? - How to generate a fixed “pseudo-random” list of doubles in a certain range the same every time? 如何向模型添加固定值属性? - How do I add a fixed value property to my model? 检查类的实例是否存在某些属性 - Check if instance of class exists with certain property 如何阻止WPF属性值继承在某些边界传播 - How to stop WPF Property Value Inheritance propagation at certain boundaries 在某些情况下如何修改委托以设置其返回值的属性 - How to modify delegate to set a property of its return value in certain cases LINQ:如何通过某些对象属性值选择列表的整个列表 - LINQ: How to select entire list of list by certain object property value 如何使用反射动态设置对象实例的属性值? - How to dynamically set the value of a property of object instance using reflection? 如何根据类的其他实例确定属性的值 - How to determine value of property based on other instance of class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM