Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeze enum value in AutoFixture

We have an enum:

enum Letters
{
    A,
    B,
    C,
    D,
    E        
}

When I try:

var frozenLetter = fixture.Freeze(Letters.D);

Strangely, frozenLetter == A.

var letter = fixture.Create<Letters>();
var anotherLetter = fixture.Create<Letters>();

Letter and anotherLetter both equal A, so the Letters type has been frozen, but to the first constant in the enum rather than the one specified.

Is there a way to freeze an enum to the constant I wish?

like image 616
jcorcoran Avatar asked Feb 06 '14 18:02

jcorcoran


1 Answers

Freeze Inject and Register are slightly different.

Use Inject for the described behavior, as the following test demonstrates:

[Fact]
public void Test()
{
    var fixture = new Fixture();

    var expected = Letters.D;
    fixture.Inject(expected);

    var letter = fixture.Create<Letters>();
    var anotherLetter = fixture.Create<Letters>();

    Assert.Equal(expected, letter);
    Assert.Equal(expected, anotherLetter);
}

The problem with the question's sample code is that the parameter (seed) isn't used as the frozen value.

like image 118
Nikos Baxevanis Avatar answered Nov 09 '22 11:11

Nikos Baxevanis