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 11using System ;
22using System . Linq . Expressions ;
3+ using NSubstitute . Core ;
34using 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
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 ;
22using NUnit . Framework ;
33
44namespace 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}
You can’t perform that action at this time.
0 commit comments