From d6336dcb04bf24bf7e15ad92cdddcdbfc24664a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 17 Jan 2026 06:24:23 +0100 Subject: [PATCH 1/2] fix: throw `ArgumentOutOfRangeException` when times of `For` is not positive --- Source/Mockolate/Setup/Callback.cs | 10 +++- .../SetupMethodTests.CallbackTests.cs | 51 +++++++++++++++++++ .../SetupPropertyTests.OnGetTests.cs | 17 +++++++ 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/Source/Mockolate/Setup/Callback.cs b/Source/Mockolate/Setup/Callback.cs index 500821c1..9b4ac1ac 100644 --- a/Source/Mockolate/Setup/Callback.cs +++ b/Source/Mockolate/Setup/Callback.cs @@ -54,7 +54,15 @@ public void When(Func predicate) /// to ). /// public void For(int times) - => _forTimes = times; + { + if (times <= 0) + { + // ReSharper disable once LocalizableElement + throw new ArgumentOutOfRangeException(nameof(times), "Times must be greater than zero."); + } + + _forTimes = times; + } /// /// Deactivates the callback after it was invoked the given number of . diff --git a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs index d7f6f52c..bb2919c1 100644 --- a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs +++ b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs @@ -319,6 +319,40 @@ public async Task CallbackWithValue_ShouldNotExecuteWhenParameterDoesNotMatch() await That(callCount).IsEqualTo(0); } + [Theory] + [InlineData(-2)] + [InlineData(0)] + public async Task For_LessThanOne_ShouldThrowArgumentOutOfRangeException(int times) + { + IReturnMethodSetupTest sut = Mock.Create(); + + void Act() + { + sut.SetupMock.Method.Method1(It.IsAny()) + .Returns("").For(times); + } + + await That(Act).Throws() + .WithMessage("Times must be greater than zero.").AsPrefix(); + } + + [Fact] + public async Task For_NegativeOrNull_ShouldThrowArgumentOutOfRangeException() + { + List invocations = []; + IReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method1(It.IsAny()) + .Do((i, _) => { invocations.Add(i); }) + .For(4); + + for (int i = 0; i < 20; i++) + { + sut.Method1(i); + } + + await That(invocations).IsEqualTo([0, 1, 2, 3,]); + } + [Fact] public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() { @@ -377,6 +411,23 @@ public async Task InParallel_ShouldInvokeParallelCallbacksAlways() await That(callCount3).IsEqualTo(6); } + [Theory] + [InlineData(-2)] + [InlineData(0)] + public async Task Only_LessThanOne_ShouldThrowArgumentOutOfRangeException(int times) + { + IReturnMethodSetupTest sut = Mock.Create(); + + void Act() + { + sut.SetupMock.Method.Method1(It.IsAny()) + .Returns("").Only(times); + } + + await That(Act).Throws() + .WithMessage("Times must be greater than zero.").AsPrefix(); + } + [Theory] [InlineData(1, 1)] [InlineData(2, 3)] diff --git a/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnGetTests.cs b/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnGetTests.cs index 704cfbe5..7ce94dc5 100644 --- a/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnGetTests.cs +++ b/Tests/Mockolate.Tests/MockProperties/SetupPropertyTests.OnGetTests.cs @@ -24,6 +24,23 @@ public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() await That(invocations).IsEqualTo([0, 1, 2, 3,]); } + [Theory] + [InlineData(-2)] + [InlineData(0)] + public async Task For_LessThanOne_ShouldThrowArgumentOutOfRangeException(int times) + { + IPropertyService sut = Mock.Create(); + + void Act() + { + sut.SetupMock.Property.MyProperty + .OnGet.Do(() => { }).For(times); + } + + await That(Act).Throws() + .WithMessage("Times must be greater than zero.").AsPrefix(); + } + [Fact] public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes() { From 8446014d3bf1ee794158cbefff38d7b1aaa56f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 17 Jan 2026 07:31:56 +0100 Subject: [PATCH 2/2] Fix review issue --- .../SetupMethodTests.CallbackTests.cs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs index bb2919c1..d0ba2019 100644 --- a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs +++ b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs @@ -336,23 +336,6 @@ await That(Act).Throws() .WithMessage("Times must be greater than zero.").AsPrefix(); } - [Fact] - public async Task For_NegativeOrNull_ShouldThrowArgumentOutOfRangeException() - { - List invocations = []; - IReturnMethodSetupTest sut = Mock.Create(); - sut.SetupMock.Method.Method1(It.IsAny()) - .Do((i, _) => { invocations.Add(i); }) - .For(4); - - for (int i = 0; i < 20; i++) - { - sut.Method1(i); - } - - await That(invocations).IsEqualTo([0, 1, 2, 3,]); - } - [Fact] public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes() {