System.IO.Abstraction找不到IStreamWriter [英] System.IO.Abstraction can't find IStreamWriter

查看:53
本文介绍了System.IO.Abstraction找不到IStreamWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对调用StreamWriter的方法进行单元测试,我试图使用System.IO.Abstraction来模拟StreamWriter,但是我也找不到最后一个Nuget上的接口,但它也对源代码进行了研究,但是不知道该用什么替代,FileInfo之类的其他东西正在按预期工作.

I am trying to unit test a method that calls StreamWriter, I am trying to use System.IO.Abstraction in order to mock StreamWriter however i can't find the interface on the last Nuget looked into the source code as well but have no idea what is the replacement for this, other stuff like FileInfo is working as expected.

谢谢

推荐答案

我也在寻找如何通过System.IO.Abstractions模拟FileStream的方法,但最初看不到它.它挂在FileInfo对象上.结果导致代码有些笨拙,并且需要强制转换.我的原始代码:

I was also looking for how to mock a FileStream via System.IO.Abstractions and couldn't see it initially. It's Hanging off the FileInfo Object. It results in slightly clunky code and required a cast. My Original Code:

FileStream fileStreamBack = null;
using (fileStreamBack = new FileStream(fileFrom, FileMode.Open, FileAccess.Read))
using (var fileStreamf = new FileStream(fileTo, FileMode.Create, FileAccess.Write))
{
                fileStreamBack.CopyTo(fileStreamf);             // Use the .Net 
                fileStreamBack.Flush(); // Making sure
                fileStreamBack.Close(); // Making sure
}

现在替换为

FileStream fileStreamBack = null;
using (fileStreamBack = (FileStream)_fileSystem.FileInfo.FromFileName(fileFrom).Open(FileMode.Open, FileAccess.Read))
using (var fileStreamf = (FileStream)_fileSystem.FileInfo.FromFileName(fileTo).Open(FileMode.Create, FileAccess.Write))
        {
            fileStreamBack.CopyTo(fileStreamf);             // Use the .Net 
            fileStreamBack.Flush(); // Making sure
            fileStreamBack.Close(); // Making sure
        }

System.IO.Abstractions.TestingHelpers中也有一个MockFileStream对象(为方便起见!)

There is also a MockFileStream object in the System.IO.Abstractions.TestingHelpers (for our convenience!)

这篇关于System.IO.Abstraction找不到IStreamWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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