亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

NSubstitute創建替代實例

標簽:
C#

创建一个替代实例的基本语法:

var substitute = Substitute.For<ISomeInterface>();

这就是通常情况下你创建一个类型的替代实例的方法。一般来说,这个类型是一个接口,但是在一些特殊情况下,也可以对类进行替代。

警告:类的替代可能会有一些不太好的副作用。NSubstitute 只能作用于类中的虚拟成员,所以类中的任何非虚成员代码将都会被真实的执行。如果你尝试替代一个类,而该类会在其构造函数或某个非虚属性中格式化硬盘,那么你就是自讨苦吃了。如果可能的话,请坚持只替代接口类型。

在知道了我们不会替代类类型之后,此处描述如何为一个具有含参构造函数的类创建替代实例:

var someClass = Substitute.For<SomeClassWithCtorArgs>(5, "hello world");

对于具有默认构造函数的类,创建其替代实例的语法与替代接口一样。

替代多个接口

有些时候,你可能需要为多个类型创建替代实例。一个最好的例子就是,当你有代码使用了某类型后,需要检查是否其实现了 IDisposable 接口,并且确认是否调用了 Dispose 进行类型销毁。


 1     public interface ICommand : IDisposable 
 2     { 3       void Execute(); 4     } 5  6     public class CommandRunner 7     { 8       private ICommand _command; 9 10       public CommandRunner(ICommand command)11       {12         _command = command;13       }14 15       public void RunCommand()16       {17         _command.Execute();18         _command.Dispose();19       }20     }21 22     [TestMethod]23     public void Test_CreatingSubstitute_MultipleInterfaces()24     {25       var command = Substitute.For<ICommand, IDisposable>();26 27       var runner = new CommandRunner(command);28       runner.RunCommand();29 30       command.Received().Execute();31       ((IDisposable)command).Received().Dispose();32     }


通过这种方法,替代实例可以实现多个类型。但请记住,一个类最多只能实现一个类。如果你愿意的话,你可以指定多个接口,但是其中只能有一个是类类型。为多个类型创建替代实例的最灵活的方式是使用重载。


 1     public class SomeClassWithCtorArgs : IDisposable 2     { 3       public SomeClassWithCtorArgs(int arg1, string arg2) 4       { 5       } 6  7       public void Dispose() { } 8     } 9 10     [TestMethod]11     public void Test_CreatingSubstitute_SpecifiedOneClassType()12     {13       var substitute = Substitute.For(14             new[] { typeof(ICommand), typeof(IDisposable), typeof(SomeClassWithCtorArgs) },15             new object[] { 5, "hello world" }16           );17       Assert.IsInstanceOfType(substitute, typeof(ICommand));18       Assert.IsInstanceOfType(substitute, typeof(IDisposable));19       Assert.IsInstanceOfType(substitute, typeof(SomeClassWithCtorArgs));20     }


替代委托

通过使用 Substiute.For<T>() 语法,NSubstitute 可以为委托类型创建替代。当为委托类型创建替代时,将无法使该替代实例实现额外的接口或类。


1     [TestMethod]2     public void Test_CreatingSubstitute_ForDelegate()3     {4       var func = Substitute.For<Func<string>>();5       func().Returns("hello");6       Assert.AreEqual<string>("hello", func());7     }
點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消