zoukankan      html  css  js  c++  java
  • NSubstitute完全手册(二)创建替代实例

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

    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     }

    NSubstitute 完全手册

  • 相关阅读:
    zabbix迁移思路
    top命令
    random随机数
    判断传入元素是否可见
    title_contains
    1.Selenium的工作原理以及网页上查找元素
    APP升级测试
    英语词汇
    作文 |素材笔记
    408计组 |二、数据的表示和运算
  • 原文地址:https://www.cnblogs.com/gaochundong/p/nsubstitute_creating_a_substitute.html
Copyright © 2011-2022 走看看