Nsubstitute full manual (11) parameter matchers

Source: Internet
Author: User

This parameter can be used to set the return value and check the received call. It provides a method to specify a call or a group of calls, so that you can set a return value for all matching calls, or check whether a matching call is received.

Ignore Parameters

You can ignore a T-type parameter by using Arg. Any <t>.

 1       Public   Interface  Icalculator  2   {  3         Int Add ( Int A, Int  B );  4         String Mode {Get ; Set  ;}  5   }  6   7   [Testmethod]  8       Public   Void  Test_argumentmatchers_ignoringarguments ()  9   {  10         VaR Calculator = substitute. For <icalculator>();  11   12 Calculator. Add (Arg. Any < Int > (), 5 ). Returns ( 7  );  13   14 Assert. areequal ( 7 , Calculator. Add ( 42 , 5  ));  15 Assert. areequal (7 , Calculator. Add ( 123 , 5  ));  16 Assert. arenotequal ( 7 , Calculator. Add ( 1 , 7  ));  17 }

In this example, we set the return value to 7 when any number is added to 5. We use Arg. Any <int> () to tell nsubstitute to ignore the first parameter.

We can also use this method to match any child type.
 1       Public  Interface  Iformatter  2   {  3         Void Format ( Object  O );  4   }  5   6   [Testmethod]  7       Public   Void Test_argumentmatchers_matchsubtypes ()  8   {  9 Iformatter formatter = substitute. For <iformatter> ();  10   11 Formatter. Format ( New   Object  ());  12 Formatter. Format ( "  Some string  " );  13   14 Formatter. inclued (). Format (Arg. Any < Object > ());  15 Formatter. inclued (). Format (Arg. Any < String > ());  16 Formatter. didnotreceive (). Format (Arg. Any < Int > ());  17 }
Parameter condition match Use Arg. is <t> (predicate <t> condition) to match a T-type parameter.
 1   [Testmethod]  2       Public   Void  Test_argumentmatchers_conditionallymatching ()  3   {  4         VaR Calculator = substitute. For <icalculator> ();  5   6 Calculator. Add ( 1 ,- 10  );  7   8         //  Check for calls with the first parameter 1 and the second parameter less than 0 received  9 Calculator. Received (). Add ( 1 , Arg. is < Int > (X => x < 0  ));  10         //  Check the number of calls that receive the first parameter 1 and the second parameter-2,-5, and-10  11  Calculator  12   . Received ()  13 . Add ( 1 , Arg. is < Int > (X => New [] {- 2 ,- 5 ,- 10  }. Contains (X )));  14         //  Check that the call with the first parameter greater than 10 and the second parameter-10 is not received. 15 Calculator. didnotreceive (). Add (Arg. is < Int > (X => x> 10 ),- 10  );  16 }

If a condition expression of a parameter throws an exception, it is assumed that the parameter is not matched and the exception itself is hidden.

 1   [Testmethod]  2       Public   Void  Test_argumentmatchers_conditionallymatchingthrowexception () 3   {  4 Iformatter formatter = substitute. For <iformatter> ();  5   6 Formatter. Format (Arg. is < String > (X => X. Length <= 10 ). Returns ( "  Matched  "  );  7   8 Assert. areequal ("  Matched  " , Formatter. Format ( "  Short  "  ));  9 Assert. arenotequal ( "  Matched  " , Formatter. Format ( "  Not matched, too long  "  ));  10  11         //  This will not match because an exception will be thrown when you try to access the null Length attribute,  12         //  Nsubstitute assumes that it does not match and hides the exception.  13 Assert. arenotequal ( "  Matched  " , Formatter. Format ( Null  ));  14 }
Match the specified parameter You can use Arg. is <t> (T value) to match the specified T-type parameter.
1   [Testmethod]  2       Public   Void  Test_argumentmatchers_matchingspecificargument ()  3   {  4         VaR Calculator = substitute. For <icalculator> ();  5   6 Calculator. Add ( 0 , 42 );  7   8         //  It may not work here. In this case, nsubstitute cannot determine which parameter to apply the matcher.  9         //  Calculator. Received (). Add (0, Arg. Any <int> ());  10   11 Calculator. Received (). Add (Arg. Is ( 0 ), Arg. Any < Int > ());  12 }

Generally, this is not necessary. In most cases, we can use 0 to replace Arg. Is (0 ). However, in some cases, nsubstitute cannot parse the token that is applied to that parameter (in fact, the parameter pair performs Fuzzy Matching, rather than directly parsing the function call ). In these cases, an ambiguousargumentsexception will be thrown, and you will be required to specify one or more extra parameter mappings. In most cases, you may have to use parameter matters for each parameter explicitly.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.