@@ -360,5 +360,63 @@ public void PerformingActionsWithArguments()
360360 }
361361 #endregion
362362 #endregion
363+
364+ #region New in NSubstitute 4+/5+
365+ [ Test ]
366+ public void ThrowsAsync_ForAsyncMethods ( )
367+ {
368+ // ThrowsAsync: throw exceptions from async methods (added in v4.4)
369+ var sub = Substitute . For < ICalculator > ( ) ;
370+
371+ sub . AddAsync ( 1 , 1 ) . ThrowsAsync ( new InvalidOperationException ( "Async failure" ) ) ;
372+
373+ Assert . ThrowsAsync < InvalidOperationException > ( async ( ) => await sub . AddAsync ( 1 , 1 ) ) ;
374+
375+ // Also works with ThrowsAsync<T>
376+ sub . AddAsync ( 2 , 2 ) . ThrowsAsync < ArgumentException > ( ) ;
377+ Assert . ThrowsAsync < ArgumentException > ( async ( ) => await sub . AddAsync ( 2 , 2 ) ) ;
378+ }
379+
380+ [ Test ]
381+ public void ArgAnyType_VerifyGenericCalls ( )
382+ {
383+ // Arg.AnyType: match any generic type parameter (added in v5.1)
384+ // Useful for verifying calls to generic methods regardless of type argument
385+ var sub = Substitute . For < ICalculator > ( ) ;
386+
387+ sub . Store ( "a" , 123 ) ;
388+ sub . Store ( "b" , "hello" ) ;
389+ sub . Store ( "c" , 3.14 ) ;
390+
391+ // Without Arg.AnyType, you'd need to verify each type separately:
392+ sub . Received ( ) . Store ( "a" , 123 ) ;
393+ sub . Received ( ) . Store ( "b" , "hello" ) ;
394+ sub . Received ( ) . Store ( "c" , 3.14 ) ;
395+
396+ // With Arg.AnyType, verify all Store calls regardless of T
397+ sub . Received ( 3 ) . Store ( Arg . Any < string > ( ) , Arg . Any < Arg . AnyType > ( ) ) ;
398+ }
399+
400+ [ Test ]
401+ public void ForTypeForwardingTo_TestSpy ( )
402+ {
403+ // Test spy: forward calls to a real implementation (added in v5.3)
404+ // Useful when you want to verify calls but still execute real logic
405+ var realCalculator = new Calculator ( ) ;
406+ var spy = Substitute . ForTypeForwardingTo < ICalculator , Calculator > ( ) ;
407+
408+ // Calls go to real implementation
409+ int result = spy . Add ( 2 , 3 ) ;
410+ Assert . That ( result , Is . EqualTo ( 5 ) ) ; // Real calculation
411+
412+ // But we can still verify calls
413+ spy . Received ( ) . Add ( 2 , 3 ) ;
414+
415+ // And override specific methods
416+ spy . Add ( 10 , 10 ) . Returns ( 999 ) ;
417+ Assert . That ( spy . Add ( 10 , 10 ) , Is . EqualTo ( 999 ) ) ; // Overridden
418+ Assert . That ( spy . Add ( 5 , 5 ) , Is . EqualTo ( 10 ) ) ; // Still real
419+ }
420+ #endregion
363421 }
364422}
0 commit comments