Use Add methods when generating lists
Context
I'm initializing an object containing with a list property using Faker. The Property is getter only, as users should never directly add a new list and instead use the Add methods to populate the list.
See the following class example
class C
{
List<object> L { get; } = new List<object>();
}
And the corresponding Fakers:
var objectFaker = new Faker<object>()
// Rules here
var classFaker = new Faker<C>()
.RuleFor(c => c.L, f => objectFaker.Generate(5);
When using the classFaker, the generated class has an empty List L, as Bogus appears to try to assign the generated list to the property (Note: This does not throw, but silently fails).
It would be awesome if Bogus would allow us to populate a list using the available Add methods.
Alternatives
Currently the only way to populate getter-only lists is to manually call the add method, which is unintuitive and unnecessarily verbose. Also it was pretty much trial and error to find this solution...
var classFaker = new Faker<C>()
.RuleFor(c => c.L, (f, c) =>
{
c.L.AddRange(objectFaker.Generate(5));
return c.L;
});
If the feature request is approved, would you be willing to submit a PR?
Possibly? My time budget is a bit tight the coming weeks...
Hi Max,
Thank you for posting an issue. Apologies for the late reply. Here's one way you could achieve the API style you're looking for. You can create an extension method to augment the Faker<T> API as shown below:
[Fact]
public void ruleforlist_addrange()
{
var hobbyFaker = new Faker<Hobby>()
.RuleFor(h => h.Id, f => f.IndexFaker)
.RuleFor(h => h.Text, f => f.Random.Word());
var userFaker = new Faker<User>()
.RuleFor(u => u.Id, f => f.IndexFaker)
.RuleFor(u => u.Name, f => f.Name.FirstName())
.RuleForList(u => u.Hobbies, f => hobbyFaker.Generate(3));
var user = userFaker.Generate(1);
user.Should().NotBeNull();
console.Dump(user);
}
public static class ExtensionsForIssue309
{
public static Faker<T> RuleForList<T, U>(this Faker<T> fakerT, Expression<Func<T, List<U>>> propertyOfListU, Func<Faker, List<U>> itemsGetter)
where T : class
{
var func = propertyOfListU.Compile();
fakerT.RuleFor(propertyOfListU, (f, t) =>
{
var list = func(t);
var items = itemsGetter(f);
list.AddRange(items);
return items;
});
return fakerT;
}
}
public class Hobby
{
public int Id { get; set; }
public string Text { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public List<Hobby> Hobbies { get; } = new List<Hobby>();
}
OUTPUT:

Amazing! I just can not figure out the resaon "return items instead of return list"
fakerT.RuleFor(propertyOfListU, (f, t) =>
{
var list = func(t);
var items = itemsGetter(f);
list.AddRange(items);
return items; //here*******************
});
anybody can explain it for me thanks I changed it for ICollection to
public static class ExtensionsForIssue309
{
public static Faker<T> RuleForICollection<T, U>(this Faker<T> fakerT, Expression<Func<T, ICollection<U>>> propertyOfListU, Func<Faker, ICollection<U>> itemsGetter)
where T : class
{
var func = propertyOfListU.Compile();
fakerT.RuleFor(propertyOfListU, (f, t) =>
{
var list = func(t);
var items = itemsGetter(f);
foreach (var item in items)
list.Add(item);
return list;
});
return fakerT;
}
}