diff --git a/src/DivertR/Record/IVerifySnapshot.cs b/src/DivertR/Record/IVerifySnapshot.cs index 352d87b8..0bfe633c 100644 --- a/src/DivertR/Record/IVerifySnapshot.cs +++ b/src/DivertR/Record/IVerifySnapshot.cs @@ -1,8 +1,12 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace DivertR.Record { public interface IVerifySnapshot : IReadOnlyList { + IVerifySnapshot ForEach(Action visitor); + + IVerifySnapshot ForEach(Action visitor); } } \ No newline at end of file diff --git a/src/DivertR/Record/Internal/VerifySnapshot.cs b/src/DivertR/Record/Internal/VerifySnapshot.cs index eb030637..3271edf2 100644 --- a/src/DivertR/Record/Internal/VerifySnapshot.cs +++ b/src/DivertR/Record/Internal/VerifySnapshot.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Collections.ObjectModel; namespace DivertR.Record.Internal @@ -8,5 +9,27 @@ internal class VerifySnapshot : ReadOnlyCollection, IVerifySnapshot public VerifySnapshot(IList items) : base(items) { } + + public IVerifySnapshot ForEach(Action visitor) + { + foreach (var item in base.Items) + { + visitor.Invoke(item); + } + + return this; + } + + public IVerifySnapshot ForEach(Action visitor) + { + var count = 0; + + foreach (var item in base.Items) + { + visitor.Invoke(item, count++); + } + + return this; + } } } \ No newline at end of file diff --git a/test/DivertR.UnitTests/RecordRedirectTests.cs b/test/DivertR.UnitTests/RecordRedirectTests.cs index 15068fe6..ab1826d4 100644 --- a/test/DivertR.UnitTests/RecordRedirectTests.cs +++ b/test/DivertR.UnitTests/RecordRedirectTests.cs @@ -816,7 +816,7 @@ public void GivenStrictModeEnabledAndRecordRedirectWithSatisfyStrictEnabled_When } [Fact] - public void GivenRecordCalls_WhenWhereCalled_ThenFiltersCalls() + public void GivenRecordCalls_WhenFilterCalled_ThenFiltersCalls() { // ARRANGE var inputs = Enumerable.Range(0, 10) @@ -836,5 +836,36 @@ public void GivenRecordCalls_WhenWhereCalled_ThenFiltersCalls() .Filter(call => call.Args.input == results[5]) .Verify(call => call.Args.input.ShouldBe(results[5])).Count.ShouldBe(1); } + + [Fact] + public void GivenRecordCalls_WhenForeachCalled_ThenIteratesCalls() + { + // ARRANGE + var inputs = Enumerable.Range(0, 10) + .Select(_ => Guid.NewGuid().ToString()) + .ToArray(); + + var calls = _fooVia + .To(x => x.Echo(Is.Any)) + .Redirect<(string input, __)>(call => call.Args.input) + .Record(); + + // ACT + var results = inputs.Select(input => _proxy.Echo(input)).ToArray(); + + // ASSERT + var count = 0; + calls.Verify() + .ForEach(call => + { + call.Returned!.Value!.ShouldBe(results[count]); + call.Returned!.Value!.ShouldBe(inputs[count++]); + }) + .ForEach((call, i) => + { + call.Returned!.Value!.ShouldBe(results[i]); + call.Returned!.Value!.ShouldBe(inputs[i]); + }).Count.ShouldBe(inputs.Length); + } } } \ No newline at end of file