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..d0ba2019 100644 --- a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs +++ b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.CallbackTests.cs @@ -319,6 +319,23 @@ 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_ShouldStopExecutingCallbackAfterTheGivenTimes() { @@ -377,6 +394,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() {