Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用File.Exists的单元测试函数_C#_Unit Testing_Mocking - Fatal编程技术网

C# 使用File.Exists的单元测试函数

C# 使用File.Exists的单元测试函数,c#,unit-testing,mocking,C#,Unit Testing,Mocking,如何对这种类型的函数进行单元测试?当然,我不能创建真正的文件,也不能对示例类和Func进行任何更改以使其更易于测试 正如我发现的,使用流行的免费框架(如moq)不可能模拟静态存在。我找到了一些模拟文件系统的框架,如System.IO.Abstracts(),但在我找到的所有示例中,方法(如CreateDirectory等-此处存在)都是从模拟对象调用的,我不确定如何使其适应此方法(如果可能的话) 创建一个FileWrapper类并将其作为依赖项传递到测试中如何 public class Exam

如何对这种类型的函数进行单元测试?当然,我不能创建真正的文件,也不能对示例类和Func进行任何更改以使其更易于测试

正如我发现的,使用流行的免费框架(如moq)不可能模拟静态
存在。我找到了一些模拟文件系统的框架,如System.IO.Abstracts(),但在我找到的所有示例中,方法(如
CreateDirectory
等-此处
存在
)都是从模拟对象调用的,我不确定如何使其适应此方法(如果可能的话)


创建一个
FileWrapper
类并将其作为依赖项传递到测试中如何

public class Example
{
    private IFileWrapper _fileWrapper;

    public Example()
        : this(new FileWrapper())
    {
    }    

    public Example(IFileWrapper fileWrapper)
    {
        _fileWrapper = fileWrapper;
    }

    public int Func()
    {
        if (_fileWrapper.Exists("some path")
        {
            // etc
        }
    }
}
然后将
FileWrapper
定义为:

internal class FileWrapper : IFileWrapper
{
    public bool Exists(string path)
    {
        return File.Exists(path);
    }
}
public interface IFileWrapper
{
    bool Exists(string path);
}
界面
IFileRapper
如下所示:

internal class FileWrapper : IFileWrapper
{
    public bool Exists(string path)
    {
        return File.Exists(path);
    }
}
public interface IFileWrapper
{
    bool Exists(string path);
}
然后,您的测试可以创建
IFileRapper
的模拟,并将其传递到您的测试类中(在此处使用Moq):

[TestMethod]
public void Func_ShouldCheckFileExists()
{
//安排
var mockFileWrapper=new Mock();
mockFileWrapper.Setup(=>wk.Exists(It.IsAny()))。返回(true);
var example=新示例(mockFileWrapper.Object);
//表演
int returnValue=example.Func(“测试路径”);
//断言
Assert.Equals(返回值,1);
}
这需要做更多的工作,但是您可以构建这样的包装器库,并在代码库中重用它们


如果你真的不能修改你的代码,那么看看微软的project或者更新的project可能会对你有所帮助。

创建一个
文件包装器
类并在测试中作为依赖项传递怎么样

public class Example
{
    private IFileWrapper _fileWrapper;

    public Example()
        : this(new FileWrapper())
    {
    }    

    public Example(IFileWrapper fileWrapper)
    {
        _fileWrapper = fileWrapper;
    }

    public int Func()
    {
        if (_fileWrapper.Exists("some path")
        {
            // etc
        }
    }
}
然后将
FileWrapper
定义为:

internal class FileWrapper : IFileWrapper
{
    public bool Exists(string path)
    {
        return File.Exists(path);
    }
}
public interface IFileWrapper
{
    bool Exists(string path);
}
界面
IFileRapper
如下所示:

internal class FileWrapper : IFileWrapper
{
    public bool Exists(string path)
    {
        return File.Exists(path);
    }
}
public interface IFileWrapper
{
    bool Exists(string path);
}
然后,您的测试可以创建
IFileRapper
的模拟,并将其传递到您的测试类中(在此处使用Moq):

[TestMethod]
public void Func_ShouldCheckFileExists()
{
//安排
var mockFileWrapper=new Mock();
mockFileWrapper.Setup(=>wk.Exists(It.IsAny()))。返回(true);
var example=新示例(mockFileWrapper.Object);
//表演
int returnValue=example.Func(“测试路径”);
//断言
Assert.Equals(返回值,1);
}
这需要做更多的工作,但是您可以构建这样的包装器库,并在代码库中重用它们


如果您真的无法修改代码,那么看看Microsoft的project或更新的project可能会对您有所帮助。

可能的话,我会再次邀请mock wheel,但下面的简单代码对我有用,它在我的许多单元测试中使用: 我在单元测试中使用public bool isUT==true,else==false; bool函数,类似于问题中的示例,是

public bool IsFileExist(string pathFile) => x.Exists(pathFile);
使用如上所述的FileWrapper

受保护的振打器x;在业务运行中为null,或者从单元测试中注入依赖项并引用模拟类

public class mock : FileOp, IFileWrapper
{
    private string myPath { get; set; }

    internal mock(string myPath = "")
    {
        base.x = this;
        this.myPath = myPath;
    }

    public bool Exists(string path) => path == myPath;
}

当myPath包含与请求中相同的字符串时,它将模拟现有文件,否则IsFileExist将返回false。

可能,我会重新邀请模拟轮,但下面的简单代码对我有效,它用于我的许多单元测试: 我在单元测试中使用public bool isUT==true,else==false; bool函数,类似于问题中的示例,是

public bool IsFileExist(string pathFile) => x.Exists(pathFile);
使用如上所述的FileWrapper

受保护的振打器x;在业务运行中为null,或者从单元测试中注入依赖项并引用模拟类

public class mock : FileOp, IFileWrapper
{
    private string myPath { get; set; }

    internal mock(string myPath = "")
    {
        base.x = this;
        this.myPath = myPath;
    }

    public bool Exists(string path) => path == myPath;
}

当myPath包含与请求中相同的字符串时,它将模拟现有文件,否则IsFileExist将返回false。

以非常简单的方式完成


Assert.IsTrue(File.Exists(FILENAME))

以非常简单的方式完成


Assert.IsTrue(File.Exists(FILENAME))

你为什么说
当然我不能创建真正的文件
?一个明确的选择是在本地计算机上进行测试。为什么不能创建真实的文件?为什么您的测试设置不能创建一个文件,而您的清理将删除它(反之亦然)?您可以使用一些商业模拟框架(例如TypeMock)模拟此类调用,但我不确定这是否是正确的解决方案……如果您无法更改示例类,如果测试失败,您将怎么办?为什么说
当然我不能创建真正的文件
?一个明确的选择是在本地计算机上进行测试。为什么不能创建真实的文件?为什么您的测试设置不能创建一个文件,而您的清理将删除它(反之亦然)?您可以使用一些商业模拟框架(例如TypeMock)模拟此类调用,但我不确定这是否是正确的解决方案……如果您无法更改示例类,如果测试失败,您将怎么做?问题是我们可以修改示例类吗?此外,IMHO,这样的解决方案会给产品代码增加不必要的复杂性,只是为了测试。。。不确定在这种特定情况下是否值得……问题是我们可以修改示例类吗?此外,IMHO,这样的解决方案会给产品代码增加不必要的复杂性,只是为了测试。。。不确定在这种特殊情况下是否值得。。。