Reputation: 95
I am using AutoFixture in my unit test, to simplify my test setup. Sometimes I have the use case that I need a list with one specific object.
var inputValues = AutoFixture.Build<TestObject>().CreateMany().ToList();
inputValues[0] = new TestObject { name = "best guy"}
Currently I solve this my issue with the code snipping below. I am sure there is a better solution.
I find this post Create a list with specific values with Autofixture C# but I am really happy this such solution.
I am also aware of something like this:
var inputValues = AutoFixture.Build<TestObject>().With(c => c.Name = "test" ).CreateMany().ToList();
But this will set all list entries with the same value.
Upvotes: 0
Views: 7676
Reputation: 1274
This question has been answered a couple times already on SO. You can see the answers here and here.
The best option is to inject the custom object/value into an existing collection. Similar to the first example you provided.
If you really wanted to, you could create a specimen builder that counts the number of times it returned a value and stop after the first one, but really this is not worth the effort. Also your test would be worse because you would make it less obvious for a reader how exactly that unique item is different from the other.
Perhaps if you would provide a more specific example, it would be easier to offer you some advice.
Upvotes: 1