File tree Expand file tree Collapse file tree 3 files changed +18
-6
lines changed
tests/NSubstitute.Acceptance.Specs Expand file tree Collapse file tree 3 files changed +18
-6
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Linq . Expressions ;
3
+ using NSubstitute . Core ;
3
4
using NSubstitute . Core . Arguments ;
5
+ using NSubstitute . Exceptions ;
4
6
5
7
// Disable nullability for client API, so it does not affect clients.
6
8
#nullable disable annotations
@@ -94,7 +96,12 @@ public static ref TDelegate InvokeDelegate<TDelegate>(params object[] arguments)
94
96
/// </summary>
95
97
public static ref T Do < T > ( Action < T > useArgument )
96
98
{
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
+
98
105
return ref ArgumentMatcher . Enqueue < T > ( new AnyArgumentMatcher ( typeof ( T ) ) , x => useArgument ( ( T ) x ! ) ) ;
99
106
}
100
107
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- using System ;
1
+ using NSubstitute . Exceptions ;
2
2
using NUnit . Framework ;
3
3
4
4
namespace NSubstitute . Acceptance . Specs ;
@@ -40,11 +40,9 @@ public void Return_result_for_any_argument()
40
40
[ Test ]
41
41
public void Throw_with_Do_AnyType ( )
42
42
{
43
- string argDoResult = null ;
44
-
45
43
ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
46
44
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 ( ) ) ) ) ;
49
47
}
50
48
}
You can’t perform that action at this time.
0 commit comments