有时,引发被替代的类型中定义的事件时非常必要的。例如下面这个例子:


在 .NET 中,事件是非常有意思的功能,因为你不能像操作其他成员似地进行传递。相反地,你只能添加或移除事件处理器,NSubstitute 正是使用了这个添加事件处理器的语法来引发事件。


在上面的例子中,我们不能真实地了解到我们所引发事件的发送者和参数,仅是知道它被调用了。在这种条件下,NSubstitute 通过为我们的事件处理器创建所需的参数,来使该操作更便捷些。


NSubstitute 不总是能够创建事件参数。如果事件的参数没有默认的构造函数,你可能不得不使用 Raise.EventWith<TEventArgs>(...) 来创建事件参数,例如下面例子中的 LowFuelWarning 事件。如果没有提供事件的发送者,则 NSubstitute 会创建。


有时事件会通过委托来声明,而没有继承自 EventHandler<> 或 EventHandler。这种事件可以通过使用 Raise.Event<TypeOfEventHandlerDelegate>(arguments)来引发。NSubstitute 会尝试和猜测该委托所需的参数,但如果没成功,NSubstitute 会告诉你具体需要提供哪个参数。
下面这个示例演示了引发 INotifyPropertyChanged 事件,该事件使用 PropertyChangedEventHandler 委托并需要2个参数。


在 IEngine 示例中,RevvedAt 事件被声明为 Action<int>。这是委托事件的另外一种形式,我们可以使用 Raise.Event<Action<int>>() 来引发该事件。


<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/22/nsubstitute_raising_events.html,如需转载请自行联系原作者