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

如何使用 MOQ 框架在 c# 中模擬靜態方法?

How to mock static methods in c# using MOQ framework?(如何使用 MOQ 框架在 c# 中模擬靜態方法?)
本文介紹了如何使用 MOQ 框架在 c# 中模擬靜態方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我最近一直在做單元測試,我已經使用 MOQ 框架和 MS Test 成功地模擬了各種場景.我知道我們無法測試私有方法,但我想知道我們是否可以使用 MOQ 模擬靜態方法.

I have been doing unit testing recently and I've successfully mocked various scenarios using MOQ framework and MS Test. I know we can't test private methods but I want to know if we can mock static methods using MOQ.

推薦答案

起訂量(和其他DynamicProxy-based 模擬框架)無法模擬任何不是虛擬或抽象方法的東西.

Moq (and other DynamicProxy-based mocking frameworks) are unable to mock anything that is not a virtual or abstract method.

只能使用基于 Profiler API 的工具來偽造密封/靜態類/方法,例如 Typemock(商業)或 Microsoft Moles(免費,在 Visual Studio 2012 Ultimate 中稱為 Fakes/2013/2015).

Sealed/static classes/methods can only be faked with Profiler API based tools, like Typemock (commercial) or Microsoft Moles (free, known as Fakes in Visual Studio 2012 Ultimate /2013 /2015).

或者,您可以重構您的設計以抽象調用靜態方法,并通過依賴注入將此抽象提供給您的類.那么您不僅會有更好的設計,而且還可以使用免費工具(例如 Moq)進行測試.

Alternatively, you could refactor your design to abstract calls to static methods, and provide this abstraction to your class via dependency injection. Then you'd not only have a better design, it will be testable with free tools, like Moq.

無需完全使用任何工具即可應用允許可測試性的通用模式.考慮以下方法:

A common pattern to allow testability can be applied without using any tools altogether. Consider the following method:

public class MyClass
{
    public string[] GetMyData(string fileName)
    {
        string[] data = FileUtil.ReadDataFromFile(fileName);
        return data;
    }
}

您可以將其包裝在 protected virtual 方法中,而不是嘗試模擬 FileUtil.ReadDataFromFile,如下所示:

Instead of trying to mock FileUtil.ReadDataFromFile, you could wrap it in a protected virtual method, like this:

public class MyClass
{
    public string[] GetMyData(string fileName)
    {
        string[] data = GetDataFromFile(fileName);
        return data;
    }

    protected virtual string[] GetDataFromFile(string fileName)
    {
        return FileUtil.ReadDataFromFile(fileName);
    }
}

然后,在您的單元測試中,從 MyClass 派生并將其命名為 TestableMyClass.然后你可以重寫 GetDataFromFile 方法來返回你自己的測試數據.

Then, in your unit test, derive from MyClass and call it TestableMyClass. Then you can override the GetDataFromFile method to return your own test data.

希望對您有所幫助.

這篇關于如何使用 MOQ 框架在 c# 中模擬靜態方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 令牌授權不起作用)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應用程序密碼從 oauth2/token 獲取訪問令牌)
New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 久久免费精彩视频 | 久久精品国产一区二区电影 | 日韩中文一区 | 国产精品免费一区二区三区四区 | 国产一区二区三区四区在线观看 | 九九九视频精品 | 荷兰欧美一级毛片 | 国产日韩一区二区三区 | 久久精品欧美电影 | 久久久国产精品视频 | 欧美成人a∨高清免费观看 色999日韩 | 在线成人免费视频 | 欧美一区二区三区视频在线播放 | 久久婷婷国产 | 综合国产第二页 | 日韩欧美国产精品综合嫩v 一区中文字幕 | 一区二区三区免费 | 成人伊人 | 亚洲欧美一区二区三区国产精品 | 国产激情在线观看视频 | 高清一区二区视频 | 日韩三| 国产成人免费一区二区60岁 | 日本一区二区不卡视频 | 一区二区精品 | 美女久久久久 | 国产精产国品一二三产区视频 | 一区二区三区四区国产 | 日本不卡免费新一二三区 | 国产一区不卡 | 自拍偷拍一区二区三区 | 成人av在线播放 | 九九九国产 | 免费高清av | 久久不卡视频 | 在线毛片网| 99精品欧美一区二区蜜桃免费 | 九九热精品视频 | xxxxxx国产| 一区二区三区免费 | 国产精品久久久久久久久久久久 |