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

您如何在 C# 中模擬文件系統(tǒng)以進行單元測試?

How do you mock out the file system in C# for unit testing?(您如何在 C# 中模擬文件系統(tǒng)以進行單元測試?)
本文介紹了您如何在 C# 中模擬文件系統(tǒng)以進行單元測試?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

是否有任何庫或方法可以在 C# 中模擬文件系統(tǒng)來編寫單元測試?在我目前的情況下,我有一些方法可以檢查某個文件是否存在并讀取創(chuàng)建日期.將來我可能需要更多.

Are there any libraries or methods to mock out the file system in C# to write unit tests? In my current case I have methods that check whether certain file exists and read the creation date. I may need more than that in future.

推薦答案

安裝 NuGet 包 System.IO.Abstractions.

Install the NuGet package System.IO.Abstractions.

最初接受此答案時,此包不存在.原始答案是針對以下歷史背景提供的:

This package did not exist when this answer was originally accepted. The original answer is provided for historical context below:

你可以通過創(chuàng)建一個界面來做到這一點:

You could do it by creating an interface:

interface IFileSystem {
    bool FileExists(string fileName);
    DateTime GetCreationDate(string fileName);
}

并創(chuàng)建一個真正的"實現(xiàn),它使用System.IO.File.Exists() 等.然后您可以使用模擬框架;我推薦 起訂量.

and creating a 'real' implementation which uses System.IO.File.Exists() etc. You can then mock this interface using a mocking framework; I recommend Moq.

有人這樣做了,請把它發(fā)布到網(wǎng)上這里.

somebody's done this and kindly posted it online here.

我已經(jīng)使用這種方法在 IClock 中模擬了 DateTime.UtcNow接口(對于我們的測試能夠控制真的非常有用時間的流動!),以及更傳統(tǒng)的 ISqlDataAccess界面.

I've used this approach to mock out DateTime.UtcNow in an IClock interface (really really useful for our testing to be able to control the flow of time!), and more traditionally, an ISqlDataAccess interface.

另一種方法可能是使用 TypeMock,這允許您攔截對類的調(diào)用并將它們存根.然而,這確實要花費錢,并且需要安裝在您整個團隊的 PC 上,并且為了運行您的構(gòu)建服務器,它顯然也不適用于System.IO.File,因為它 不能存根 mscorlib.

Another approach might be to use TypeMock, this allows you to intercept calls to classes and stub them out. This does however cost money, and would need to be installed on your whole team's PCs and your build server in order to run, also, it apparently won't work for the System.IO.File, as it can't stub mscorlib.

您也可以接受某些方法不可單元測試并在單獨的慢速集成/系統(tǒng)測試中測試它們套房.

You could also just accept that certain methods are not unit testable and test them in a separate slow-running integration/system tests suite.

這篇關于您如何在 C# 中模擬文件系統(tǒng)以進行單元測試?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(liá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(如何獲取守護進程或服務器到 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(使用電子郵件地址和應用程序密碼從 oauth2/token 獲取訪問令牌)
New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 應用程序在通過管理門戶更新之前無法運行)
主站蜘蛛池模板: 香蕉超碰| 一级日批片| 日韩a在线 | 国产一二三区精品视频 | 亚洲国产中文字幕 | 亚洲国产精品一区二区三区 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 天堂色 | 中文字幕一区二区三区乱码在线 | 亚洲精品一二区 | 视频一二三区 | 成人一区二 | 久久男人| 亚洲欧美在线一区 | 亚洲国产aⅴ精品 | 精品视频999 | 天天狠狠| 在线免费观看成年人视频 | 欧美午夜一区二区三区免费大片 | 精品一区国产 | 亚洲毛片在线观看 | 国产精品一区二区在线播放 | 久草久草久草 | 丁香婷婷久久久综合精品国产 | 欧美日韩国产一区二区三区 | 久久综合伊人 | 成人亚洲视频 | 美女二区 | 国产在线一区二区三区 | 玖玖久久 | 91精品一区二区三区久久久久 | 国产在线视频一区二区 | 日本一区二区三区免费观看 | 欧美精品网站 | 亚洲一区二区视频 | 欧美成人a∨高清免费观看 欧美日韩中 | 欧美一级特黄aaa大片在线观看 | 九九综合| 亚洲天堂成人在线视频 | 国产在线一区二区三区 | 羞羞视频一区二区 |