Skip to content

Commit ce576e1

Browse files
authored
refactor: improve concurrent test cases for mock setup (#607)
This pull request updates unit tests and test helpers to improve test reliability and correctness, especially around method and indexer setup matching. The most significant changes include fixing the logic for determining the latest matching setup in tests and introducing a new `FakeMethodMatch` helper class. **Test logic improvements:** * Updated `GetLatestOrDefault_ShouldReturnLatestMatching` in `MockSetupsTests.MethodSetupsTests.cs` to check that the latest added matching setup is returned, aligning with expected behavior. * Refactored `ThreadSafety_ConcurrentAddsAndQueries_ShouldReturnConsistentMatches` in `MockSetupsTests.IndexerSetupsTests.cs` to more accurately track and assert the latest matching setup under concurrent conditions, removing unnecessary helper methods and using `Interlocked.Exchange` for thread safety. **Test helper enhancements:** * Added a new `FakeMethodMatch` class to `TestHelpers/FakeMethodSetup.cs` to allow configurable matching logic for method setups in tests. * Updated `FakeMethodSetup` to use `FakeMethodMatch` for more flexible and accurate test setup. **Other:** * Added missing `System.Threading` using directive to `MockSetupsTests.IndexerSetupsTests.cs` to support thread-safe operations.
1 parent 33ee188 commit ce576e1

3 files changed

Lines changed: 16 additions & 20 deletions

File tree

Tests/Mockolate.Internal.Tests/MockSetupsTests.IndexerSetupsTests.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public async Task Stress_ShouldMaintainCountAfterManyAdds()
3939
public async Task ThreadSafety_ConcurrentAddsAndQueries_ShouldReturnConsistentMatches()
4040
{
4141
MockSetups.IndexerSetups setups = new();
42-
FakeIndexerSetup matchSetup = new(true);
43-
setups.Add(matchSetup);
42+
FakeIndexerSetup initialMatch = new(true);
43+
setups.Add(initialMatch);
4444
Parallel.For(0, 200, i =>
4545
{
4646
bool shouldMatch = i % 2 == 0;
@@ -49,24 +49,13 @@ public async Task ThreadSafety_ConcurrentAddsAndQueries_ShouldReturnConsistentMa
4949
FakeIndexerAccess access = new();
5050
_ = setups.GetLatestOrDefault(access);
5151
});
52-
FakeIndexerSetup expected = GetExpectedMatchingSetup(setups);
52+
FakeIndexerSetup finalMatch = new(true);
53+
setups.Add(finalMatch);
5354
FakeIndexerAccess finalAccess = new();
54-
IndexerSetup? result = setups.GetLatestOrDefault(finalAccess);
55-
56-
await That(result).IsSameAs(expected);
5755

58-
static FakeIndexerSetup GetExpectedMatchingSetup(MockSetups.IndexerSetups setups)
59-
{
60-
IndexerSetup? latest = setups.GetLatestOrDefault(new FakeIndexerAccess());
61-
if (latest is FakeIndexerSetup { ShouldMatch: true, } castLatest)
62-
{
63-
return castLatest;
64-
}
56+
IndexerSetup? result = setups.GetLatestOrDefault(finalAccess);
6557

66-
FakeIndexerSetup matching = new(true);
67-
setups.Add(matching);
68-
return matching;
69-
}
58+
await That(result).IsEqualTo(finalMatch);
7059
}
7160

7261
[Fact]

Tests/Mockolate.Internal.Tests/MockSetupsTests.MethodSetupsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public async Task GetLatestOrDefault_ShouldReturnLatestMatching()
2929
setups.Add(setup1);
3030
setups.Add(setup2);
3131

32-
MethodSetup? result = setups.GetLatestOrDefault(s => s == setup1);
32+
MethodSetup? result = setups.GetLatestOrDefault(_ => true);
3333

34-
await That(result).IsEqualTo(setup1);
34+
await That(result).IsEqualTo(setup2);
3535
}
3636

3737
[Fact]

Tests/Mockolate.Internal.Tests/TestHelpers/FakeMethodSetup.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@
33

44
namespace Mockolate.Internal.Tests.TestHelpers;
55

6+
internal sealed class FakeMethodMatch : IMethodMatch
7+
{
8+
private readonly bool _matches;
9+
internal FakeMethodMatch(bool matches) { _matches = matches; }
10+
public bool Matches(MethodInvocation methodInvocation) => _matches;
11+
}
12+
613
internal sealed class FakeMethodSetup : MethodSetup
714
{
8-
internal FakeMethodSetup() : base(null!) { }
15+
internal FakeMethodSetup() : base(new FakeMethodMatch(true)) { }
916
protected override bool? GetSkipBaseClass() => null;
1017
protected override T SetOutParameter<T>(string parameterName, Func<T> defaultValueGenerator) => default!;
1118
protected override T SetRefParameter<T>(string parameterName, T value, MockBehavior behavior) => value;

0 commit comments

Comments
 (0)