如何使用 System.IO.Abstraction 模拟 FileStream? [英] How to mock FileStream with System.IO.Abstraction?

查看:22
本文介绍了如何使用 System.IO.Abstraction 模拟 FileStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 System.IO.Abstraction 项目与 System.IO.Abstraction.TestingHelpers 一起模拟 FileStream.

I'm trying to use the System.IO.Abstraction project along with System.IO.Abstraction.TestingHelpers to mock a FileStream.

这是使用我要测试的文件流的代码:

This is the code that's using the file stream that I want to test:

private readonly IFileSystem _fileSystem;

public void ExtractImageAndSaveToDisk(IXLPicture xlPicture, string filePath)
{
    using (MemoryStream ms = new MemoryStream())
    {
        xlPicture.ImageStream.CopyTo(ms);

        using (FileStream fs = (FileStream)_fileSystem.FileStream.Create(filePath, FileMode.Create))
        {
            ms.CopyTo(fs);
            fs.Flush();
            fs.Close(); 
        }
    }
}

这就是我设置测试的方式:

And this is how I've set up the testing:

[TestMethod]
public void CheckFileIsSavedToDisk()
{
    // Arrange
    var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
    {
        { @"c:\Test\Images\", new MockDirectoryData() },
    });

    var xlPicture = GetXLPicture();
    var filePath = @"c:\Test\Images\myimage.jpeg";

    var sut = new CostSheetImageExtractor(mockFileSystem);

    // Act
    sut.ExtractImagesAndSaveToDisk(xlPicture, filePath);
}

运行,我得到异常:

System.InvalidCastException: Unable to cast object of type 'System.IO.Abstractions.TestingHelpers.MockFileStream' to type 'System.IO.FileStream'.

using (FileStream fs = ... 行.

第一个想法是我需要更改 FileStream fs 以使用真实和模拟对象共享的接口,但据我所知,没有 IFileStreamFileStreamMockFileStream 共享的 code> 接口,所以我猜我这样做完全错误?有没有办法用 System.IO.Abstraction 测试这段代码?

The first thought was that I need to change the FileStream fs to use an interface that both the real and mock objects share, but as far as I can see there's no IFileStream interface that FileStream and MockFileStream share, so I'm guessing I'm doing this completely wrong? Is there actually a way to test this code with System.IO.Abstraction?

这个现有答案似乎表明这应该是可能的,我也尝试这样做,但得到了相同的结果.

This existing answer seems to suggest this should be possible, I tried doing it that way too but got the same results.

推荐答案

不嘲笑文件系统是迄今为止最好的选择,正如 Chris F Carroll 的回答所说,但如果你真的想这样做,您的问题几乎包含答案.

Not mocking the filesystem is by far the best option, as Chris F Carroll answer says, but if you want to really do that, your question almost contains the answer.

正如您所说,没有 IFileStream 接口这样的东西,但是如果您查看 FileStream的定义你会发现它继承了抽象类Stream,它是各种流数据源的通用基类.假的 MockFileStream 也应该从那里继承.

As you said, there is no such thing as an IFileStream interface, but if you look at the definition of FileStream you'll find that it inherits the abstract class Stream, and that's a common base class for all kinds of streamed data sources. The fake MockFileStream should also inherit from there.

因此,尝试更改您的代码以使用抽象类:

So, try changing your code to use the abstract class instead:

using (Stream fs = (Stream)_fileSystem.FileStream.Create(filePath, FileMode.Create))
{
    ms.CopyTo(fs);
    fs.Flush();
    fs.Close(); 
}

它仍然保留所有基本方法,而您的代码只使用基本方法.

It still retain all the base methods and your code just uses the base ones.

这篇关于如何使用 System.IO.Abstraction 模拟 FileStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆