久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Moq 中模擬被測主題的方法?

Mock a method of the subject under test in Moq?(在 Moq 中模擬被測主題的方法?)
本文介紹了在 Moq 中模擬被測主題的方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我想測試我班級的方法 A,但不調(diào)用通常由 A 調(diào)用的實際方法 B.這是因為 B 有很多外部交互,我現(xiàn)在不想測試.

I want to test method A of my class, but without calling the actual method B which is normally called by A. That's because B has a lot of external interactions I don't want to test for now.

我可以為 B 調(diào)用的所有服務(wù)創(chuàng)建模擬,但這是相當(dāng)多的工作.我寧愿只是模擬 B 并讓它返回示例數(shù)據(jù).

I could create mocks for all the services called by B, but that's quite some work. I'd rather just mock B and make it return sample data.

這可能與 Moq 框架有關(guān)嗎?

Is that possible to do with Moq framework?

推薦答案

是的,有一個問題!您必須確保方法 B 是虛擬的并且可以被覆蓋.

It is, with a catch! You have to make sure method B is virtual and can be overriden.

然后,將模擬設(shè)置為在未提供設(shè)置時調(diào)用基本方法.然后你設(shè)置B,但不設(shè)置A.因為沒有設(shè)置A,實際的實現(xiàn)會被調(diào)用.

Then, set the mock to call the base methods whenever a setup is not provided. Then you setup B, but don't setup A. Because A was not setup, the actual implementation will be called.

var myClassMock = new Mock<MyClass>();
myClassMock.Setup(x => x.B()); //mock B

myClassMock.CallBase = true;

MyClass obj = myClassMock.Object;
obj.A(); // will call the actual implementation
obj.B(); // will call the mock implementation

在幕后,Moq 將動態(tài)創(chuàng)建一個擴展 MyClass 并覆蓋 B 的類.

Behinds the scenes, Moq will dynamically create a class that extends MyClass and overrides B.

這篇關(guān)于在 Moq 中模擬被測主題的方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權(quán)不起作用)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務(wù)器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發(fā)技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調(diào)用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應(yīng)用程序密碼從 oauth2/token 獲取訪問令牌)
New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 應(yīng)用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 久久精品伊人 | 国产激情一区二区三区 | 欧美日韩一区在线播放 | 日本精品视频一区二区 | 欧美成人一区二免费视频软件 | 国产成人福利视频 | 中文成人无字幕乱码精品 | 日韩三级电影一区二区 | 精品在线一区 | 乳色吐息在线观看 | 亚洲精品国产电影 | 一区二区三区视频在线 | 日韩高清一区二区 | 国产在线精品一区二区 | 成人在线中文字幕 | 浴室洗澡偷拍一区二区 | 国产精品免费在线 | 欧美炮房| 国产中文 | 久久精品欧美一区二区三区不卡 | 精品国产一区久久 | 日韩av中文 | 欧美综合国产精品久久丁香 | 欧美精品久久 | 成人免费区一区二区三区 | 日韩在线精品视频 | 日韩一区二区福利视频 | 国产一区二区麻豆 | 免费av播放| 国产成人av电影 | 国产一二区免费视频 | 色屁屁在线观看 | 亚洲国产日韩欧美 | 成人欧美一区二区三区视频xxx | 亚洲乱码国产乱码精品精98午夜 | 免费一区二区 | 美女啪啪国产 | 一区二区三区中文字幕 | 亚洲九九 | 夜久久| 日韩在线精品视频 |