public PNDTicketNumberIsUniqueValidatorTests()
{
_fixture = new Fixture().Customize(new AutoMoqCustomization());
_validator = new PNDTicketNumberIsUniqueValidator();
var pnd = _fixture.Build<PenaltyNotice>()
.OmitAutoProperties()
.Create();
_fixture.Inject(pnd);
_model = _fixture.Build<CaseModel>().OmitAutoProperties().With(w => w.PenaltyNoticeDisorders).Create();
_model.PenaltyNotices[0].IdNumber = "12345";
_model.PenaltyNotices[1].IdNumber = "4654";
_model.PenaltyNotices[2].IdNumber = "87745";
}
After this setup, every penalty notice has the same IdNumber.
After stepping through the code, before any IdNumber assignment they are all null. After the first assignment they are all 12345. After the second they are all 4654 After the third they are all 87745
I want to be able to assign specific values to specific properties... Is there enough information here to see why this wouldn't be working, or for ideas where to look?
I didn’t spot it at first, but by using
_fixture.Inject(pnd);
...you’re basically saying to AutoFixture: “whenever you need to create a PenaltyNotice
, use (the single instance) pnd
”. Hence you end up with the same instance in every member of your collection.
What you probably intended was instead
_fixture.Register<PenaltyNotice>(() => _fixture
.Build<PenaltyNotice>()
.OmitAutoProperties()
.Create());
Similar to Inject
, but now each time AutoFixture creates a PenaltyNotice
it uses that function to create a new instance instead.