1
Vote

Is this a bug?

description

[Test]
public void ThisTestPasses()
{
    var fixture = new Fixture();
    var expectedProfileID = fixture.Create<int>();

    var p1 = fixture.Build<Profile>().With(p => p.ID, expectedProfileID).Create();

    Assert.AreEqual(p1.ID, expectedProfileID);
}

[Test]
public void ButThisOneFails()
{
    var fixture = new Fixture();
    var expectedProfileID = fixture.Create<int>();

    var p1 = fixture.Build<Profile>().With(p => p.ID, expectedProfileID).Create();
    var p2 = fixture.Freeze<IProfile>(c => c.FromFactory(() => p1));

    Assert.AreEqual(p1.ID, expectedProfileID);
}

public interface IProfile
{
    int ID { get; set; }
}

public class Profile : IProfile
{
    public int ID { get; set; }
}

comments

bartelink wrote Oct 6, 2013 at 11:17 PM

Firstly I'd say its definitely not a coding slip or thing the devs are not aware of.

FromFactory by default will rewrite all the properties (it has no special knowledge / makes no attempt to delve into the definition of the incoming object)

You can .OmitAutoProperties() to prevent this effect.

People with more in depth knowledge will be along in due course to address the reasoning as to why this is by design and/or might be improved upon in the furture

ploeh wrote Oct 7, 2013 at 6:27 AM

Freeze doesn't do what you think it does. You'll need to use Inject instead.

bartelink wrote Oct 8, 2013 at 1:24 AM

@ryansimmen FYI looks like your flagging hasnt gone unnoticed:- https://github.com/AutoFixture/AutoFixture/issues/151#issuecomment-25785651