Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Source/Mockolate/Setup/Callback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ public void When(Func<int, bool> predicate)
/// to <see langword="true" />).
/// </remarks>
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;
}

/// <summary>
/// Deactivates the callback after it was invoked the given number of <paramref name="times" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IReturnMethodSetupTest>();

void Act()
{
sut.SetupMock.Method.Method1(It.IsAny<int>())
.Returns("").For(times);
Comment on lines +331 to +332
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test validates the For method but uses Returns("") which tests a different code path than the callback-focused Do() method. Consider adding a test that uses .Do(() => { }).For(times) to match the property setup test pattern and ensure validation works consistently across both setup approaches.

Copilot uses AI. Check for mistakes.
}

await That(Act).Throws<ArgumentOutOfRangeException>()
.WithMessage("Times must be greater than zero.").AsPrefix();
}

[Fact]
public async Task For_ShouldStopExecutingCallbackAfterTheGivenTimes()
{
Expand Down Expand Up @@ -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<IReturnMethodSetupTest>();

void Act()
{
sut.SetupMock.Method.Method1(It.IsAny<int>())
.Returns("").Only(times);
Comment on lines +406 to +407
Copy link

Copilot AI Jan 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test validates the Only method but uses Returns("") which tests a different code path than the callback-focused Do() method. Consider adding a test that uses .Do(() => { }).Only(times) to match the property setup test pattern and ensure validation works consistently across both setup approaches.

Copilot uses AI. Check for mistakes.
}

await That(Act).Throws<ArgumentOutOfRangeException>()
.WithMessage("Times must be greater than zero.").AsPrefix();
}

[Theory]
[InlineData(1, 1)]
[InlineData(2, 3)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPropertyService>();

void Act()
{
sut.SetupMock.Property.MyProperty
.OnGet.Do(() => { }).For(times);
}

await That(Act).Throws<ArgumentOutOfRangeException>()
.WithMessage("Times must be greater than zero.").AsPrefix();
}

[Fact]
public async Task For_WithWhen_ShouldStopExecutingCallbackAfterTheGivenTimes()
{
Expand Down