使用具有许多构造函数参数的内部JSON构造函数在AutoFixture 4.8.0中创建公共类型

问题描述 投票:2回答:1

与在AutoFixture 4.8.0中一样,有没有更好的替代方法来使用Fixture.Register注册一个函数来创建一个只暴露internal构造函数的类型?

In context:

我正在开发一个.net标准类库来构建供内部使用的API。我们不希望暴露公共构造函数,因此这些模型的所有构造函数都是internal。属性另外public readonly。这些类将通过Json反序列化进行实例化,在这个特定的库中,有一个需要考虑> 20个属性的模型。作为奖励,我们希望尽可能使用[AutoDataAttribute]。

即。是否可以使用AutoFixture提供内部jsonconstructor,其中包含非复杂类型(也包含内部)和20多个参数的混合?

[TestFixture]
public sealed class Tests
{
    private Fixture _fixture;

    [OneTimeSetUp]
    public void OneTimeSetup()
    {
        _fixture = new Fixture();

        _fixture.Register(() => new Vehicle(_fixture.Create<string>(), _fixture.Create<int>()));

        _fixture.Register(
            () => new Driver(_fixture.Create<string>(), _fixture.Create<int>(), _fixture.Create<Vehicle>()));
    }

    [Test]
    public void CanCreateDriverWithVehicle()
    {
        Func<Driver> func = () => _fixture.Create<Driver>();
        func.Should().NotThrow(); // Green
    }
}

[PublicAPI]
public sealed class Driver
{
    public readonly string Name;
    public readonly int Age;
    public Vehicle Vehicle;

    [JsonConstructor]
    internal Driver(string name, int age, Vehicle vehicle)
    {
        Name = name;
        Age = age;
        Vehicle = vehicle;
    }
}

[PublicAPI]
public sealed class Vehicle
{
    public readonly string Manufacturer;
    public readonly int Miles;

    [JsonConstructor]
    internal Vehicle(string manufacturer, int miles)
    {
        Manufacturer = manufacturer;
        Miles = miles;
    }
}
c# autofixture
1个回答
2
投票

默认情况下,AutoFixture仅考虑公共构造函数。它不会接收内部构造函数,即使它们可以从测试执行程序集访问(甚至不确定是否有合理的技术可能性来检查)。

您可以轻松自定义夹具以支持它。从以下代码示例中获取灵感:

[Fact]
public void ShouldActivateTypesWithInternalConstructor()
{
    var fixture = new Fixture();

    fixture.ResidueCollectors.Add(
        new Postprocessor(
            new MethodInvoker(
                new ModestInternalConstructorQuery()),
            new AutoPropertiesCommand()
        ));

    var result = fixture.Create<TypeWithInternalConstructor>();

    Assert.NotEqual(0, result.Property);
}

public class ModestInternalConstructorQuery : IMethodQuery
{
    public IEnumerable<IMethod> SelectMethods(Type type)
    {
        if (type == null) throw new ArgumentNullException(nameof(type));

        return from ci in type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic)
            where ci.IsAssembly // Take internal constructors only
            let parameters = ci.GetParameters()
            where parameters.All(p => p.ParameterType != type)
            orderby parameters.Length ascending
            select new ConstructorMethod(ci) as IMethod;
    }
}

public class TypeWithInternalConstructor
{
    public int Property { get; }

    internal TypeWithInternalConstructor(int property)
    {
        Property = property;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.