NSubstitute完全手册(十八)检查调用顺序

注:目前这个功能还在 NSubstitute.Experimental 名空间下,我们还在对其 API 和行为进行试验。非常欢迎你来试一试,但是要注意其可能在后续的版本中有变化。请在讨论组中反馈意见。

有时调用需要满足特定的顺序。就像已知的 "Temporal Coupling",其取决于调用收到的时间。理想情况下,我们可能会修改设计来移除这些耦合。但当不能移除时,凭借 NSubstitute 我们可以断言调用的顺序。

 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     }

如果接收到调用的顺序不同,Received.InOrder 会抛出异常,并显示期待的结果和实际的调用结果。

我们也可以使用标准的参数匹配器来匹配调用,就像当我们需要检查单个调用时一样。

 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     }

NSubstitute 完全手册

posted @ 2013-05-22 13:26  sangmado  阅读(1293)  评论(0编辑  收藏  举报