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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?