From d06df04d58921a94f52d44016d3b2d0e6324eb7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 24 Jan 2026 20:54:09 +0100 Subject: [PATCH 1/2] refactor: remove redundant check in `ReturnMethodSetup` --- Source/Mockolate/Setup/ReturnMethodSetup.cs | 20 ----- .../SetupMethodTests.ReturnsThrowsTests.cs | 73 +++++++++++++++++++ 2 files changed, 73 insertions(+), 20 deletions(-) diff --git a/Source/Mockolate/Setup/ReturnMethodSetup.cs b/Source/Mockolate/Setup/ReturnMethodSetup.cs index 0247918d..0668f668 100644 --- a/Source/Mockolate/Setup/ReturnMethodSetup.cs +++ b/Source/Mockolate/Setup/ReturnMethodSetup.cs @@ -420,11 +420,6 @@ protected override TResult GetReturnValue(MethodInvocation invocation, Func defaultValueGenerator) where TResult : default { - if (_returnCallbacks.Count == 0) - { - return defaultValueGenerator(); - } - if (!TryCast(invocation.Parameters[0].Value, out T1 p1, behavior)) { throw new MockException( @@ -709,11 +704,6 @@ protected override TResult GetReturnValue(MethodInvocation invocation, Func defaultValueGenerator) where TResult : default { - if (_returnCallbacks.Count == 0) - { - return defaultValueGenerator(); - } - if (!TryCast(invocation.Parameters[0].Value, out T1 p1, behavior)) { throw new MockException( @@ -1013,11 +1003,6 @@ protected override TResult GetReturnValue(MethodInvocation invocation, Func defaultValueGenerator) where TResult : default { - if (_returnCallbacks.Count == 0) - { - return defaultValueGenerator(); - } - if (!TryCast(invocation.Parameters[0].Value, out T1 p1, behavior)) { throw new MockException( @@ -1335,11 +1320,6 @@ protected override TResult GetReturnValue(MethodInvocation invocation, Func defaultValueGenerator) where TResult : default { - if (_returnCallbacks.Count == 0) - { - return defaultValueGenerator(); - } - if (!TryCast(invocation.Parameters[0].Value, out T1 p1, behavior)) { throw new MockException( diff --git a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs index d6b17dfb..41b1fad4 100644 --- a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs +++ b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs @@ -199,6 +199,18 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() await That(result).IsEqualTo("foo"); } + [Fact] + public async Task SetupWithReturn_ShouldUseBaseValue() + { + ReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method0() + .Returns("bar"); + + string result = sut.Method0(); + + await That(result).IsEqualTo("bar"); + } + [Fact] public async Task Throws_Callback_ShouldThrowException() { @@ -491,6 +503,18 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() await That(result).IsEqualTo("foo-1"); } + [Fact] + public async Task SetupWithReturn_ShouldUseBaseValue() + { + ReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method1(It.IsAny()) + .Returns("bar"); + + string result = sut.Method1(1); + + await That(result).IsEqualTo("bar"); + } + [Fact] public async Task Throws_Callback_ShouldThrowException() { @@ -818,6 +842,18 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() await That(result).IsEqualTo("foo-1-2"); } + [Fact] + public async Task SetupWithReturn_ShouldUseBaseValue() + { + ReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) + .Returns("bar"); + + string result = sut.Method2(1, 2); + + await That(result).IsEqualTo("bar"); + } + [Fact] public async Task Throws_Callback_ShouldThrowException() { @@ -1151,6 +1187,18 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() await That(result).IsEqualTo("foo-1-2-3"); } + [Fact] + public async Task SetupWithReturn_ShouldUseBaseValue() + { + ReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny()) + .Returns("bar"); + + string result = sut.Method3(1, 2, 3); + + await That(result).IsEqualTo("bar"); + } + [Fact] public async Task Throws_Callback_ShouldThrowException() { @@ -1486,6 +1534,18 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() await That(result).IsEqualTo("foo-1-2-3-4"); } + [Fact] + public async Task SetupWithReturn_ShouldIgnoreBaseValue() + { + ReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method4(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()) + .Returns("bar"); + + string result = sut.Method4(1, 2, 3, 4); + + await That(result).IsEqualTo("bar"); + } + [Fact] public async Task Throws_Callback_ShouldThrowException() { @@ -1833,6 +1893,19 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() await That(result).IsEqualTo("foo-1-2-3-4-5"); } + [Fact] + public async Task SetupWithReturn_ShouldIgnoreBaseValue() + { + ReturnMethodSetupTest sut = Mock.Create(); + sut.SetupMock.Method.Method5(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny()) + .Returns("bar"); + + string result = sut.Method5(1, 2, 3, 4, 5); + + await That(result).IsEqualTo("bar"); + } + [Fact] public async Task Throws_Callback_ShouldThrowException() { From 7b40c13abde8b9c7f41764883d3c34e528d76f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 24 Jan 2026 20:56:18 +0100 Subject: [PATCH 2/2] Fix review issues --- .../MockMethods/SetupMethodTests.ReturnsThrowsTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs index 41b1fad4..b5a5615b 100644 --- a/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs +++ b/Tests/Mockolate.Tests/MockMethods/SetupMethodTests.ReturnsThrowsTests.cs @@ -200,7 +200,7 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() } [Fact] - public async Task SetupWithReturn_ShouldUseBaseValue() + public async Task SetupWithReturn_ShouldReturnSetupValue() { ReturnMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method0() @@ -504,7 +504,7 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() } [Fact] - public async Task SetupWithReturn_ShouldUseBaseValue() + public async Task SetupWithReturn_ShouldReturnSetupValue() { ReturnMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method1(It.IsAny()) @@ -843,7 +843,7 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() } [Fact] - public async Task SetupWithReturn_ShouldUseBaseValue() + public async Task SetupWithReturn_ShouldReturnSetupValue() { ReturnMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method2(It.IsAny(), It.IsAny()) @@ -1188,7 +1188,7 @@ public async Task SetupWithoutReturn_ShouldUseBaseValue() } [Fact] - public async Task SetupWithReturn_ShouldUseBaseValue() + public async Task SetupWithReturn_ShouldReturnSetupValue() { ReturnMethodSetupTest sut = Mock.Create(); sut.SetupMock.Method.Method3(It.IsAny(), It.IsAny(), It.IsAny())