Nsubstitute full manual (18) Check the Call Sequence

Source: Internet
Author: User

Note: Currently, this function is still in the nsubstitute. Experimental namespace, and we are still experimenting with its API and behavior. You are welcome to give it a try, but note that it may be changed in later versions. Please give feedback in the discussion group.

Sometimes the call must meet the specific order. Like the known "temporal coupling", it depends on the time when the call is received. Ideally, we may modify the design to remove these coupling. But when it cannot be removed, we can use nsubstitute to assert the call order.

 1       Public   Class  Controller  2   {  3         Private  Iconnection connection;  4         Private Icommand command;  5         Public  Controller (iconnection connection, icommand command)  6   {  7           This . Connection = Connection;  8           This . Command = Command;  9   }  10  11         Public   Void  Dostuff ()  12   {  13   Connection. open ();  14   Command. Run (connection );  15   Connection. Close ();  16   }  17   } 18   19       Public   Class  Icommand  20   {  21         Public   Void  Run (iconnection connection ){}  22   }  23   24       Public   Class Iconnection  25   {  26         Public   Void  Open (){}  27   28         Public   Void  Close (){}  29   30         Public   Event  Action somethinghappened; 31   }  32   33   [Testmethod]  34       Public   Void  Test_checkingcallorder_commandrunwhileconnectionisopen ()  35   {  36         VaR Connection = substitute. For <iconnection> ();  37         VaR Command = substitute. For <icommand> ();  38         VaR Subject = New  Controller (connection, command );  39   40   Subject. dostuff ();  41   42 Received. Inorder () => 43   {  44   Connection. open (); 45   Command. Run (connection );  46   Connection. Close ();  47   });  48 }

If the order of received calls is different, the received ed. Inorder will throw an exception and display the expected results and the actual call results.

We can also use the standard parameter matching to match the call, just as when we need to check a single call.

 1   [Testmethod]  2       Public   Void Test_checkingcallorder_subscribetoeventbeforeopeningconnection ()  3   {  4         VaR Connection = substitute. For <iconnection> ();  5 Connection. somethinghappened + = () => { /*  Some event handler  */  };  6   Connection. open ();  7  8 Received. Inorder () => 9   {  10 Connection. somethinghappened + = Arg. Any <action> ();  11   Connection. open ();  12   });  13 }

 

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.