Skip to content

Commit

Permalink
Fixed Generating Random Team Members (#41)
Browse files Browse the repository at this point in the history
* fixed non repeatable picking when generating random members
  • Loading branch information
skrasekmichael authored Mar 13, 2024
1 parent 348b99b commit 95c3bbc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static TeamGenerator WithRandomMembers(this TeamGenerator teamGenerator,

return teamGenerator
.RuleFor(TeamGenerators.TEAM_MEMBERS_FIELD, (f, t) => f
.Make(count, index => f.PopRandom(new List<User>(pot))
.Make(() => new List<User>(pot), count, (list, index) => f.PopRandom(list)
.Map(user => TeamGenerators.TeamMember
.RuleForBackingField(tm => tm.TeamId, t.Id)
.RuleForBackingField(tm => tm.UserId, user.Id)
Expand Down
6 changes: 6 additions & 0 deletions tests/TeamUp.Tests.Common/Extensions/FakerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public static T PopRandom<T>(this Faker faker, List<T> list) where T : class
return elem;
}

public static IList<TOut> Make<TOut, TListElem>(this Faker _, Func<List<TListElem>> prepareList, int count, Func<List<TListElem>, int, TOut> action)
{
var list = prepareList();
return Enumerable.Range(1, count).Select((index, elem) => action(list, index)).ToList();
}

public static Faker<T> UsePrivateConstructor<T>(this Faker<T> faker) where T : class
=> faker.CustomInstantiator(f => (Activator.CreateInstance(typeof(T), nonPublic: true) as T)!);

Expand Down

0 comments on commit 95c3bbc

Please sign in to comment.