Jan Paolo Go
Jan Paolo Go

Reputation: 6542

Is there a way to populate an object instance using AutoFixture?

Here is a sample of what I'm trying to do

var foo = CreateFoo(); // I don't have access to the internals of CreateFoo

fixture.Populate(foo); // foo should now have random property values

Obviously, there is no Fixture.Populate method. But how should I solve this problem? Thanks!

Upvotes: 2

Views: 1137

Answers (1)

Alex Povar
Alex Povar

Reputation: 734

AutoFixture indeed doesn't provide API to fill writable properties of an existing object. However, you can use the following snippet to achieve the desired behavior:

var fixture = new Fixture();
var objectToFill = new TypeWithProperties();

new AutoPropertiesCommand().Execute(objectToFill, new SpecimenContext(fixture));

Assert.NotNull(objectToFill.Value);

If you need that often, you can create an extension method for the IFixture in your test project.

Upvotes: 4

Related Questions