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 完全手册

转载于:https://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_checking_call_order.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值