小编Dir*_*gen的帖子

单元测试中的Mock File.Exists方法(C#)

可能重复:
.NET文件系统包装程序库

我想写一个测试文件内容加载的测试.在示例中,用于加载内容的类是

FileClass
Run Code Online (Sandbox Code Playgroud)

和方法

GetContentFromFile(string path).
Run Code Online (Sandbox Code Playgroud)

有没有办法嘲笑

File.exists(string path)
Run Code Online (Sandbox Code Playgroud)

使用moq的给定示例中的方法?

例:

我有一个类有这样的方法:

public class FileClass 
{
   public string GetContentFromFile(string path)
   {
       if (File.exists(path))
       {
           //Do some more logic in here...
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

.net c# unit-testing moq

7
推荐指数
1
解决办法
8859
查看次数

标签 统计

.net ×1

c# ×1

moq ×1

unit-testing ×1