lundi 23 novembre 2015

Create an EF entity stub with AutoFixture

For example I've got these partial classes that was generated by EF Database First:

Dog: (EF entity)

public partial class Dog
{
    public int DogID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public int PetOwnerID { get; set; }
    // Navigation property
    public virtual PetOwner PetOwner { get; set; }
}

PetOwner: (EF entity)

public partial class PetOwner
{
    public int PetOwnerID { get; set; }
    public string PetOwnerName { get; set; }
    // Navigation property
    public virtual ICollection<Dog> Dogs { get; set; }
}

I need a simple stub of Dog type for unit testing. But when I try to generate a stub using AutoFixture a recursive dependency exception throws. If I try to change fixture behavior like this, it hangs on.

var fixture = new Fixture();
        fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList().ForEach(b => fixture.Behaviors.Remove(b));
        fixture.Behaviors.Add(new OmitOnRecursionBehavior(1));
        var a = fixture.Create<Dog>();

I don't need any EF functionality here, just a simple class with a properties to test. I've got NUnit, Moq, AutoFixture.

Aucun commentaire:

Enregistrer un commentaire