C# Autofixture生成不带重复项的集合(按ID)

C# Autofixture生成不带重复项的集合(按ID),c#,.net,testing,autofixture,C#,.net,Testing,Autofixture,我有以下课程: public class Foo { public List<DescriptionInfo> Descriptions { get; set; } } public class DescriptionInfo { public int LanguageId { get; set; } public string Value { get; set; } } 无效实例: Foo1: - LanguageId: 1, Value: "_04

我有以下课程:

public class Foo
{
    public List<DescriptionInfo> Descriptions { get; set; }
}


public class DescriptionInfo
{
    public int LanguageId { get; set; }
    public string Value { get; set; }
}
无效实例:

Foo1:
 - LanguageId: 1, Value: "_04dcd6"
 - LanguageId: 1, Value: "_66ccc4"
 - LanguageId: 2, Value: "zuzulu_c05b0f"

首先,让我建议你的POCE可以更精确。如果列表中不允许使用相同的
LanguageID
进行描述,则可以按照以下方式重新构建POCE:

public class Foo
{
    public ISet<DescriptionInfo> Descriptions { get; set; }
}

public class DescriptionInfo
{
    public int LanguageID { get; set; }
    public string Value { get; set; }

    public override bool Equals(object obj)
    {
        var anotherInfo = (DescriptionInfo)obj;
        return anotherInfo.LanguageID == LanguageID;
    }

    public override int GetHashCode()
    {
        return LanguageID;
    }
}

最后一点注意-您的代码片段是错误的-字符串比较区分大小写;因此,它应该是
info.Name==“LanguageId”
,而且最好检查声明属性的类型,而不仅仅是属性名称。

因为您希望ID值在单个
Foo
实例上是唯一的,所以您最好自定义
Foo
类型本身

您可能希望让AutoFixture处理
Foo
的其他部分,除了
DescriptionInfo
值。一种方法是创建
ISpecimenCommand
,该命令可用作生成值的后处理器:

public class UniqueIDsOnFooCommand : ISpecimenCommand
{
    private static readonly int[] languageIds = new [] { 1, 2, 666, };
    private static readonly Random random = new Random();

    public void Execute(object specimen, ISpecimenContext context)
    {
        var foo = specimen as Foo;
        if (foo == null)
            return;

        foreach (var t in
            foo.Descriptions.Zip(Shuffle(languageIds), Tuple.Create))
        {
            var description = t.Item1;
            var id = t.Item2;
            description.LanguageId = id;
        }            
    }

    private static IEnumerable<T> Shuffle<T>(IReadOnlyCollection<T> source)
    {
        return source.OrderBy(_ => random.Next());
    }
}
以下测试通过:

[Fact]
public void CreateFoos()
{
    var fixture = new Fixture().Customize(new FooCustomization());
    fixture.Behaviors.Add(new TracingBehavior());

    var foos = fixture.CreateMany<Foo>();

    Assert.True(
        foos.All(f => f
            .Descriptions
            .Select(x => x.LanguageId)
            .Distinct()
            .Count() == f.Descriptions.Count),
        "Languaged IDs should be unique for each foo.");
}

综上所述,.

有效语言ID列表包含三个值。如果创建四个
Foo
实例,会发生什么?我添加了一些示例。该要求适用于特定的Foo实例,而不是全球范围。感谢您的详细回答。确实存在一个案例错误&检查声明类型是一个非常好的主意。也是一个很好的解决方案,即使在我看来,这比@ondrej svejdar答案的“hacky”要少一点。不过,因为他是第一个,我会把他的回答保持为被接受的。@piotrwest不必担心。重要的是你得到了一个有用的答案。
public class Foo
{
    public ISet<DescriptionInfo> Descriptions { get; set; }
}

public class DescriptionInfo
{
    public int LanguageID { get; set; }
    public string Value { get; set; }

    public override bool Equals(object obj)
    {
        var anotherInfo = (DescriptionInfo)obj;
        return anotherInfo.LanguageID == LanguageID;
    }

    public override int GetHashCode()
    {
        return LanguageID;
    }
}
fixture.Customizations.Add(new TypeRelay(typeof(ISet<DescriptionInfo>),
  typeof(HashSet<DescriptionInfo>)));
fixture.Customizations.Add(new LanguageIdSpecimenBuilder());
var foo = fixture.Create<Foo>();
public object Create(object request, ISpecimenContext context)
{
    var info = request as PropertyInfo;
    if (info != null && info.Name == "Descriptions" && info.DeclaringType == typeof(Foo))
    {
        if (info.Name == "Descriptions")
        {
            return context.Create<List<DescriptionInfo>>()
             .GroupBy(g => g.LanguageId)
             .Select(g => g.First())
             .ToList();
         }
    }
    if (info != null && info.Name == "LanguageId" && info.DeclaringType == typeof(DescriptionInfo))
    {
        return LanguageIds.GetRandomElement();
    }
    return new NoSpecimen(request);
}
public class UniqueIDsOnFooCommand : ISpecimenCommand
{
    private static readonly int[] languageIds = new [] { 1, 2, 666, };
    private static readonly Random random = new Random();

    public void Execute(object specimen, ISpecimenContext context)
    {
        var foo = specimen as Foo;
        if (foo == null)
            return;

        foreach (var t in
            foo.Descriptions.Zip(Shuffle(languageIds), Tuple.Create))
        {
            var description = t.Item1;
            var id = t.Item2;
            description.LanguageId = id;
        }            
    }

    private static IEnumerable<T> Shuffle<T>(IReadOnlyCollection<T> source)
    {
        return source.OrderBy(_ => random.Next());
    }
}
public class FooCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customizations.Add(
            SpecimenBuilderNodeFactory.CreateTypedNode(
                typeof(Foo),
                new Postprocessor(
                    new MethodInvoker(
                        new ModestConstructorQuery()),
                    new CompositeSpecimenCommand(
                        new AutoPropertiesCommand(),
                        new UniqueIDsOnFooCommand()))));
    }
}
[Fact]
public void CreateFoos()
{
    var fixture = new Fixture().Customize(new FooCustomization());
    fixture.Behaviors.Add(new TracingBehavior());

    var foos = fixture.CreateMany<Foo>();

    Assert.True(
        foos.All(f => f
            .Descriptions
            .Select(x => x.LanguageId)
            .Distinct()
            .Count() == f.Descriptions.Count),
        "Languaged IDs should be unique for each foo.");
}
  Foo:
  - LanguageId: 1, Value: "Valueefd1268c-56e9-491a-a43d-3f385ea0dfd8"
  - LanguageId: 666, Value: "Value461d7130-7bc0-4e71-96a8-432c019567c9"
  - LanguageId: 2, Value: "Valuee2604a80-f113-485c-8276-f19a97bca505"

  Foo:
  - LanguageId: 2, Value: "Value66eee598-1895-4c0d-b3c6-4262711fe394"
  - LanguageId: 666, Value: "Valuef9a58482-13c6-4491-a690-27b243af6404"
  - LanguageId: 1, Value: "Valueeb133071-dad7-4bff-b8ef-61d3505f1641"

  Foo:
  - LanguageId: 1, Value: "Valuea1161dc6-75e5-45a3-9333-f6bca01e882b"
  - LanguageId: 666, Value: "Value31332272-5b61-4d51-8572-172b781e5f6b"
  - LanguageId: 2, Value: "Value83f80569-277d-49b2-86e5-be256075835e"