具有实体框架的AutoFixture - 将int映射到枚举

时间:2015-07-23 12:11:11

标签: c# entity-framework unit-testing enums autofixture

我想对我的Entity Framework(6.1.3)数据模型使用AutoFixture(3.30.8)。我已经实现了AutoFixture.AutoEF(0.3.5)包来帮助修复实体并避免关系生成的循环引用。

但是,我的表有几个int列,在代码中由枚举表示,我希望能够根据枚举值设置int值,并为每个枚举值设置一个代理类。

以下是我的架构的一个简化示例:

public partial class MyContext : DbContext
{
    public virtual DbSet<Parent> Parents { get; set; }
    public virtual DbSet<Child> Children { get; set; }
}

public partial class Parent
{
    public Parent()
    {
        this.Children = new HashSet<Child>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public int Status { get; set; }

    public virtual ICollection<Child> Children { get; set; }
}

public partial class Child
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
    public int Type { get; set; }

    public virtual Parent Parent { get; set; }
}

以下是我的枚举:

public enum Status
{
    Active = 1,
    Inactive = 2
}

public enum Type
{
    Up = 1,
    Down = 2,
    Left = 3,
    Right = 4
}

以下是我创建代理的方式:

var fixture = new Fixture();

fixture.Customize(new EntityCustomization(new DbContextEntityTypesProvider(typeof(MyContext))));

var parents = fixture.CreateMany<Parent>();

这是因为我有3个Parent类的集合,每个类有3个Child类,Id属性很好地匹配。但是,正如预期的那样,StatusType属性是由AutoFixture生成的随机整数。

我想要的是2 Parent个班级,其中一个Status 1个,Status 2个,{有4个Child个类,每个类都有Type 1234

这可以使用AutoFixture自动进行吗?

编辑:为了让我更清楚我的要求:

如何自动将我的类上的int属性映射到枚举,以便为映射的枚举的每个值获取一个代理类。

这也需要在类有2个或更多映射枚举的情况下工作。

e.g。如果Child具有TypeStatus属性,我预计每Children有8 Parent个:

Status = 1, Type = 1
Status = 1, Type = 2
Status = 1, Type = 3
Status = 1, Type = 4
Status = 2, Type = 1
Status = 2, Type = 2
Status = 2, Type = 3
Status = 2, Type = 4

要进一步推断,如果Parent同时包含StatusType,我会期望8 Parent个代理类,每个代理类有8个Child代理类

编辑2:以下是我的手动编码替代生成器的外观示例,如果我在两个类上都放置了两个枚举。通过使用AutoFixture,我可以自动化除循环之外的所有内容,以生成Enums的每个排列。 就是我要问的怎么做。

public class Substitutes
{
    private int parentIdSeed;

    private int childIdSeed;

    public Substitutes()
    {
        this.parentIdSeed = 0;

        this.childIdSeed = 0;

        this.Parents = new List<Parent>();

        this.Children = new List<Child>();

        this.GenerateParents();
    }

    private void GenerateParents()
    {
        foreach (Type type in Enum.GetValues(typeof(Type)))
        {
            foreach (Status status in Enum.GetValues(typeof(Status)))
            {
                this.parentIdSeed++;

                var parent = new Parent { Id = this.parentIdSeed, Name = "Parent " + this.parentIdSeed, Status = (int)status, Type = (int)type };

                this.GenerateChildren(parent);

                this.Parents.Add(parent);
            }
        }
    }

    private void GenerateChildren(Parent parent)
    {
        foreach (Type type in Enum.GetValues(typeof(Type)))
        {
            foreach (Status status in Enum.GetValues(typeof(Status)))
            {
                this.childIdSeed++;

                var child = new Child { Id = this.childIdSeed, Name = "Child " + this.childIdSeed, Status = (int)status, Type = (int)type, Parent = parent, ParentId = parent.Id };

                parent.Children.Add(child);

                this.Children.Add(child);
            }
        }
    }

    public List<Child> Children { get; set; }

    public List<Parent> Parents { get; set; }
}

1 个答案:

答案 0 :(得分:1)

是的,这是可能的:

var parents = fixture.CreateMany<Parent>(2).ToList();

parents[1].Status = 1;
parents[1].Children = fixture.CreateMany<Child>(4).ToList();
parents[1].Children.ElementAt(1).Type = 1;
parents[1].Children.ElementAt(2).Type = 2;
parents[1].Children.ElementAt(3).Type = 3;
parents[1].Children.ElementAt(4).Type = 4;

parents[2].Status = 2;
parents[2].Children = fixture.CreateMany<Child>(4).ToList();
parents[2].Children.ElementAt(1).Type = 1;
parents[2].Children.ElementAt(2).Type = 2;
parents[2].Children.ElementAt(3).Type = 3;
parents[2].Children.ElementAt(4).Type = 4;