有時,引發被替代的類型中定義的事件時非常必要的。例如下面這個例子:


在 .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,如需轉載請自行聯系原作者