Nsubstitute full manual (12) Use callback functions and whendo syntax

Source: Internet
Author: User

Sometimes some code can be executed when a special call is receivedCodeIs very useful. We have seen similar code in the return value setting section of the function.

     Public   Interface  Icalculator {  Int Add ( Int A, Int  B );  String Mode { Get ; Set  ;} [Testmethod]  Public   Void Test_callbackswhendo_passfunctionstoreturns (){  VaR Calculator = substitute. For <icalculator> ();  VaR Counter = 0  ; Calculator. Add (  0 , 0  ). Returnsforanyargs (x => 0  ). Anddoes (x => Counter ++ ); Calculator. Add (  7 , 3  ); Calculator. Add (  2 , 2  ); Calculator. Add (  11 ,- 3  ); Assert. areequal (counter,  3  );} 

To pass function parametersUse the function to set the return valueThis section provides more details.

Creates a callback for a call with no return value

Returns () can be used to set the callback function for the member to generate the return value. However, for a void Member, we need different methods, because we cannot call a void and return a value. In this case, we can use the when .. do syntax.

When called, do this.

When... Do uses two calls to configure callback. First, call the replace instance's when () method to pass a function. The parameter of this function is to replace the instance itself, and then we can call the members we need here, even if this member returns void. Then call the do () method to pass a callback. When the substitute instance Member is called, execute the callback.

 1       Public   Interface  Ifoo  2   {  3         Void Sayhello ( String  To );  4   }  5   6  [Testmethod]  7       Public   Void  Test_callbackswhendo_usewhendo ()  8   {  9         VaR Counter = 0  ;  10         VaR Foo = substitute. For <ifoo> ();  11   12 Foo. When (x => X. sayhello ( "  World  "  ))  13 . Do (x => counter ++ );  14   15 Foo. sayhello ( "  World  "  );  16 Foo. sayhello ( "  World "  );  17 Assert. areequal ( 2  , Counter );  18 }

The call information contained in the parameters passed to the do () method is the same as that passed to the returns () callback. These call information can be used to access the parameters.

Note that we can also use the when .. do syntax for non-void members. However, we generally recommend the returns () syntax because it is more concise and clear. You may find that for a non-void function, this function is very useful when you want to execute a function without changing the previous return value.

 [Testmethod]  Public   Void  Test_callbackswhendo_usewhendoonnonvoid (){  VaR Calculator = substitute. For <icalculator> ();  VaR Counter = 0  ; Calculator. Add (  1 , 2 ). Returns ( 3  ); Calculator. When (x => X. Add (Arg. Any < Int > (), Arg. Any < Int > (). Do (x => Counter ++ ); VaR Result = calculator. Add ( 1 , 2  ); Assert. areequal (  3  , Result); Assert. areequal (  1  , Counter );} 
Create callback for each parameter

If we only need to create a callback for a special parameter in some places, we may use the method for creating a callback for each parameter, such as Arg. do () and Arg. invoke (), instead of using when .. do.

Parameter callback gives us more concise code. Other APIs in nsubstitute also maintain this style. For more information and examples, see the operation on parameters section.

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.