Reputation: 150
I'm coding some tests using AutoFixture in C# 8.0 but I'm had been problems, I guess, about class inheritance.
For example, I have these classes:
public class Parent
{
public Guid Id {get; protected set;}
public int Age {get; protected set;}
public void SetAge(int age)
{
this.Age = age;
}
}
and the child class
public class Child: Parent
{
public string name {get; private set;}
private Child() //I Need this because Migrations
{
}
}
After it, I use AutoFixture to test my class:
var fixture = new Fixture();
fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
fixture.Behaviors
.OfType<ThrowingRecursionBehavior>()
.ToList()
.ForEach(b => fixture.Behaviors.Remove(b));
fixture.Register<Parent>(() => null);
fixture.Behaviors.Add(new OmitOnRecursionBehavior(recursionDepth: 3));
fixture.Customize<Child>(c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));
fixture.Customize<Child>(ce => ce.Do(x => x.SetAge(10));
var childObject = fixture.Create<Child>();
and BOOM! I get an error at the moment when the fixture tries to create the object
"AutoFixture was unable to create an instance from the project, most likely because it has no public constructor, is an abstract or non-public type.
If I change private for the public constructor in Child class, when I set the Age, the object is created Empty. If I don't use SetAge method, the object is created without problems.
Someone had this problem? Do I need to set some property in AutoFixture when I Use inheritance classes?
Thank you
Upvotes: 0
Views: 1002
Reputation: 150
Alright, babe!
I found this:
Force AutoFixture to use the greediest constructor
And Gotcha! I solve my problem!
Upvotes: 0