天天看点

NSubstitute完全手册(二)创建替代实例

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

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

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

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

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

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

NSubstitute完全手册(二)创建替代实例
NSubstitute完全手册(二)创建替代实例

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

NSubstitute完全手册(二)创建替代实例
NSubstitute完全手册(二)创建替代实例

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

NSubstitute完全手册(二)创建替代实例
NSubstitute完全手册(二)创建替代实例

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/21/nsubstitute_get_started.html">(一)入门基础</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/21/nsubstitute_creating_a_substitute.html">(二)创建替代实例</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/21/nsubstitute_setting_a_return_value.html">(三)设置返回值</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/21/nsubstitute_return_for_specific_args.html">(四)为特定参数设置返回值</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/21/nsubstitute_return_for_any_args.html">(五)为任意参数设置返回值</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/21/nsubstitute_return_from_a_function.html">(六)使用函数设置返回值</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/21/nsubstitute_multiple_return_values.html">(七)设置多个返回值</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_replacing_return_values.html">(八)替换返回值</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_checking_received_calls.html">(九)检查接收到的调用</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_clearing_received_calls.html">(十)清理已收到的调用</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_argument_matchers.html">(十一)参数匹配器</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_callbacks_void_calls_and_when_do.html">(十二)使用回调函数和WhenDo语法</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_throwing_exceptions.html">(十三)抛出异常</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_raising_events.html">(十四)引发事件</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_auto_and_recursive_mocks.html">(十五)自动递归模拟</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_setting_out_and_ref_args.html">(十六)设置out和ref参数</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_actions_with_argument_matchers.html">(十七)参数匹配器上的操作</a>

<a href="http://www.cnblogs.com/gaochundong/archive/2013/05/22/nsubstitute_checking_call_order.html">(十八)检查调用顺序</a>

本文转自匠心十年博客园博客,原文链接:http://www.cnblogs.com/gaochundong/archive/2013/05/21/nsubstitute_creating_a_substitute.html,如需转载请自行联系原作者

继续阅读