Nsubstitute full manual (7) set multiple return values

Source: Internet
Author: User

You can also configure to return different values in multiple calls. The following example demonstrates this function by calling properties. The call methods are the same.

 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_multiplereturnvalues_returnmultiplevalues ()  9   {  10         VaR Calculator = substitute. For <icalculator> ();  11  12 Calculator. mode. Returns ( "  Dec  " , "  HEX  " , "  Bin  "  );  13 Assert. areequal ( "  Dec  "  , Calculator. mode ); 14 Assert. areequal ( "  HEX  "  , Calculator. mode );  15 Assert. areequal ( "  Bin  "  , Calculator. mode );  16 }

Of course, this method can also be done by using the function to return results, but passing multiple values to returns () seems easier and more readable.

Using callback to return multiple values returns () also supports passing multiple functions that create returned results, which allows you to call or throw exceptions in a series of calls or execute some actions.
 1   [Testmethod] 2 [Expectedexception ( Typeof  (Exception)]  3       Public   Void  Test_multiplereturnvalues_usingcallbacks ()  4   {  5         VaR Calculator = substitute. For <icalculator> ();  6   7 Calculator. mode. Returns (x =>"  Dec  " , X => "  HEX  " , X => { Throw   New  Exception ();});  8 Assert. areequal ( "  Dec  "  , Calculator. mode );  9 Assert. areequal ("  HEX  "  , Calculator. mode );  10         VaR Result = Calculator. mode;  11 }

 

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.