7

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 dog = 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.

UPDATE:

var dog = fixture.Build<Dog>().Without(x => x.PetOwner).Create();

This solves the problem, but I need a navigation property to be not null.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
AsValeO
  • 2,859
  • 3
  • 27
  • 64
  • @Enrico Campidoglio, test just never finishes. I'm trying to collect more information now. – AsValeO Nov 23 '15 at 11:56
  • Don't update your Q including your answers inside it. If you find a working answer for your own question, answer it yourself, and accept it as valid ( I think you have to wait for a pair of days to do so). That makes it useful for the community. And, if, as in this case, you partially solve the problem, you can edit this Q, answer it, and create a new Q with the new problem. It's a little bit more work, but it's much better. – JotaBe Nov 23 '15 at 12:12
  • Do you need the `PetOwner.Dogs` collection to be populated as well? – Enrico Campidoglio Nov 23 '15 at 12:54
  • @Enrico Campidoglio, nope, I need just `Dog`'s `PetOwner` navigation property with `PetOwnerID` and `PetOwnerName`. – AsValeO Nov 23 '15 at 12:57

1 Answers1

6

I wasn't able to reproduce the error. This test passes just fine using AutoFixture 3.36.12:

[Test]
public void CreateEntityWithNavigationProperty()
{
    var fixture = new Fixture();
    fixture.Behaviors.Add(new OmitOnRecursionBehavior());

    var dog = fixture.Create<Dog>();

    Assert.That(dog.PetOwner, Is.Not.Null);
    Assert.That(dog.PetOwner.Dogs, Is.Empty);
}

However, as a workaround, you can explicitly customize AutoFixture to create objects of type PetOwner without populating the PetOwner.Dogs property:

[Test]
public void CreateEntityWithNavigationProperty()
{
    var fixture = new Fixture();
    fixture.Customize<PetOwner>(c =>
        c.With(owner => owner.Dogs, Enumerable.Empty<Dog>()));

    var dog = fixture.Create<Dog>();

    Assert.That(dog.PetOwner, Is.Not.Null);
    Assert.That(dog.PetOwner.Dogs, Is.Empty);
}

This yields the same result as the previous test, where the PetOwner.Dogs property is set to an empty sequence, which is much better than null.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • Thanks a lot, Enrico! Yep,now I've got this working too. `Customize<>` looks cool. I still have an issue in my real more complex code, with many dependencies, I'll supplement this useful info if I find something helpful. – AsValeO Nov 23 '15 at 13:27