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 }