Skip to content

Commit 93e2524

Browse files
committed
Fixed RedundantArgumentMatcherException
1 parent 055fb2b commit 93e2524

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/NSubstitute/Arg.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Linq.Expressions;
3+
using NSubstitute.Core;
34
using NSubstitute.Core.Arguments;
5+
using NSubstitute.Exceptions;
46

57
// Disable nullability for client API, so it does not affect clients.
68
#nullable disable annotations
@@ -94,7 +96,12 @@ public static ref TDelegate InvokeDelegate<TDelegate>(params object[] arguments)
9496
/// </summary>
9597
public static ref T Do<T>(Action<T> useArgument)
9698
{
97-
if (typeof(T) == typeof(AnyType)) throw new ArgumentException("Use DoForAny() instead of Do<AnyType>()");
99+
if (typeof(T) == typeof(AnyType))
100+
{
101+
SubstitutionContext.Current.ThreadContext.DequeueAllArgumentSpecifications();
102+
throw new DoAnyTypeException();
103+
}
104+
98105
return ref ArgumentMatcher.Enqueue<T>(new AnyArgumentMatcher(typeof(T)), x => useArgument((T) x!));
99106
}
100107

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NSubstitute.Exceptions;
2+
3+
public class DoAnyTypeException : SubstituteException
4+
{
5+
private const string FixedMessage = "Use DoForAny() instead of Do<AnyType>()";
6+
public DoAnyTypeException() : base(FixedMessage) { }
7+
}

tests/NSubstitute.Acceptance.Specs/GenericArguments.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using NSubstitute.Exceptions;
22
using NUnit.Framework;
33

44
namespace NSubstitute.Acceptance.Specs;
@@ -40,11 +40,9 @@ public void Return_result_for_any_argument()
4040
[Test]
4141
public void Throw_with_Do_AnyType()
4242
{
43-
string argDoResult = null;
44-
4543
ISomethingWithGenerics something = Substitute.For<ISomethingWithGenerics>();
4644

47-
Assert.Throws<ArgumentException>(() =>
48-
something.Log(Arg.Any<int>(), Arg.Do<Arg.AnyType>(a => argDoResult = a.ToString())));
45+
Assert.Throws<DoAnyTypeException>(() =>
46+
something.Log(Arg.Any<int>(), Arg.Do<Arg.AnyType>(a => _ = a.ToString())));
4947
}
5048
}

0 commit comments

Comments
 (0)