Server.MapPath 的单元测试 [英] Unit testing for Server.MapPath

查看:33
本文介绍了Server.MapPath 的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法.从硬盘中检索文档.我无法从单元测试中对此进行测试.它总是抛出异常无效的空路径或其他东西.如何测试.我暂时为单元测试创​​建了会话.但我不能为这个 Server.MapPath.如何做到这一点?

I've a method. which retrieve a document from hard disk. I can't test this from unit testing. It always throw an exception invalid null path or something. How to test that. I've temporarily created session for unit test. But I can't for this Server.MapPath. How to do that ?

推荐答案

你可以在 Server.MapPath 上使用依赖注入和抽象

You can use dependancy injection and abstraction over Server.MapPath

public interface IPathProvider
{
   string MapPath(string path);
}

生产实施将是:

public class ServerPathProvider : IPathProvider
{
     public string MapPath(string path)
     {
          return HttpContext.Current.Server.MapPath(path);
     }
}

在测试一个时:

public class TestPathProvider : IPathProvider
{
    public string MapPath(string path)
    {
        return Path.Combine(@"C:project",path);
    }
}

这篇关于Server.MapPath 的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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