我如何 MOQ System.IO.FileInfo 类...或任何其他没有接口的类? [英] How do I MOQ the System.IO.FileInfo class... or any other class without an interface?

查看:17
本文介绍了我如何 MOQ System.IO.FileInfo 类...或任何其他没有接口的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我创建的记录器类编写许多单元测试,我想模拟文件类.我找不到我需要用来创建 MOQ 的接口......那么你如何成功地为没有接口的类起订量?

I am writing a number of unit tests for a logger class I created and I want to simulate the file class. I can't find the interface that I need to use to create the MOQ... so how do you successfully MOQ a class without an interface?

我也不清楚如何在没有可用接口的情况下使用依赖注入:

It also isn't clear to me how I can use dependency injection without having an interface available:

private FileInfo _logFile;

public LogEventProcessorTextFile(FileInfo logFile) {
    _logFile = logFile;
}

当我真的想做这样的事情时(注意 IFileInfo 而不是 FileInfo):

When I really want to do something like this (note IFileInfo instead of FileInfo):

private IFileInfo _logFile;

public LogEventProcessorTextFile(IFileInfo logFile) {
    _logFile = logFile;
}

推荐答案

使用 SystemWrapper,一个库它为许多本身不实现接口的 .NET 类提供接口和可模拟的包装类.

Use SystemWrapper, a library which provides interfaces and mockable wrappers classes for many .NET classes which don't implement interfaces themselves.

这篇关于我如何 MOQ System.IO.FileInfo 类...或任何其他没有接口的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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