gt.guybrush
gt.guybrush

Reputation: 1378

How to tell Autofixture to create objects with different ID?

i have a simple Setup method in my tests thats creates two instances of an object (make by and Id and a Description properties) and i have done it with autofixture:

MyObject o1 = fixture.Create<MyObject>();
MyObject o2 = fixture.Create<MyObject>();

next i try to save objects to db but i get error of duplicate key, i debug the setup and see that o1 and o2 have the same Id

According to Wiki, number should be generate progressivly:

Autogenerated Number

int autoGeneratedNumber = fixture.Create<int>();

Sample Result

int: 1, followed by 2, then by 3, etc.

but seem id does not work in this way with object property so now i use this simple workaround:

MyObject o1= fixture.Build<MyObject>().With(x => x.Id, 1).Create();
MyObject o2= fixture.Build<MyObject>().With(x => x.Id, 2).Create();

but don't like it very much

is here a way to use ISpecimenBuilder for setting up Autofixture to make it generate progressive id?

Some more info:

this is my base test class:

public class BaseDBTest
{       
    public BaseDBTest()
    { }
    public Ploeh.AutoFixture.Fixture fixture { get { return new Fixture(); } }
}

and test setup:

[TestFixture]
public class MyObjectTests : BaseDBTest
{
    MyObject o1;
    MyObject o2;

    [TestFixtureSetUp]
    public void CreaDati()
    {
        o1= fixture.Create<MyObject >();
        o2= fixture.Create<MyObject >();
    }
}

strange things is:

if I debug of the specific test objects are created with different id and random, but if I debug of all the tests of my project (with visual studio 2013 using Nunit runner) id's are created equal

EDIT2

MyObject definition, quite complex, sorry:

public class MyObject: LookUpObject<MyObject, int>
{
}

public abstract class LookUpObject<TObject, TKeyType> : EquatableObject<TObject>,     IKeyedEntity<TKeyType>
    where TObject : class
    where TKeyType : struct
{
    private TKeyType id;
    private string description;
    private bool isValid;

    public virtual TKeyType Id
    {
        get { return id; }
        set { id = value; }
    }

    public virtual string Description
    {
        get { return description; }
        set { description= value; }
    }
    public virtual bool IsValid
    {
        get { return isValid; }
        set { isValid= value; }
    }

    protected LookUpObject()
    {           
    }
}

EDIT 3

image of the strange things make with Nunit (I was afraid it might depend on Visual Studio),

single test run link

project test run link

Upvotes: 9

Views: 13386

Answers (1)

demoncodemonkey
demoncodemonkey

Reputation: 11957

This is because in your base class property to get the Fixture, you are returning a new Fixture object every time. The auto generation of Ids can only be guaranteed per Fixture instance.

Change this:

public class BaseDBTest
{       
    public BaseDBTest()
    { }
    public Ploeh.AutoFixture.Fixture fixture { get { return new Fixture(); } }
}

to this:

public class BaseDBTest
{     
    private Fixture _fixture = new Fixture();
    public BaseDBTest()
    { }
    public Ploeh.AutoFixture.Fixture fixture { get { return _fixture; } }
}

Upvotes: 9

Related Questions