天天看點

NSubstitute完全手冊(一)入門基礎

然後就可以建立一個新的測試 Fixture(可以選擇使用你最喜歡的UT測試架構,本文涉及的示例中我們使用MSTest),開始思考從哪裡入手。

首先,添加 using NSubstitute; 到目前的C#代碼檔案中,有了它我們就可以開始建立替身了。

現在,比如我們有一個非常簡單的電腦接口:

我們可以讓NSubstitute來建立類型執行個體的替代執行個體。可以建立諸如 Stub、Mock、Fake、Spy、Test Double 等,但當我們隻是想要一個能有一定程度控制的替代執行個體時,為什麼我們要困擾于此呢?

現在,我們可以告訴被建立的替代執行個體,當方法被調用時傳回一個值:

NSubstitute完全手冊(一)入門基礎
NSubstitute完全手冊(一)入門基礎

我們可以檢查該替代執行個體是否接收到了一個指定的調用,或者未收到某指定調用:

NSubstitute完全手冊(一)入門基礎
NSubstitute完全手冊(一)入門基礎

如果 Received() 斷言失敗,NSubstitute 會嘗試給出有可能是什麼問題:

NSubstitute完全手冊(一)入門基礎
NSubstitute完全手冊(一)入門基礎

我們也可以對屬性使用與方法類似的 Retures() 文法,或者繼續使用簡單的屬性 setter 來設定傳回值。

NSubstitute完全手冊(一)入門基礎
NSubstitute完全手冊(一)入門基礎

NSubstitute 支援參數比對功能,可以設定參數規則,并斷言判斷是否接收到參數比對的調用。

NSubstitute完全手冊(一)入門基礎
NSubstitute完全手冊(一)入門基礎

我們也可以在使用參數比對功能的同時,傳遞一個函數給 Returns() ,以此來使替代執行個體具有更多的功能。

NSubstitute完全手冊(一)入門基礎
NSubstitute完全手冊(一)入門基礎

Returns() 也可通過構造一個傳回值序列來指定多個參數。

NSubstitute完全手冊(一)入門基礎
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_get_started.html,如需轉載請自行聯系原作者