【发布时间】:2023-03-18 16:50:01
【问题描述】:
假设我有一个具有以下扩展方法的静态类,它从缓存中检索文件或从磁盘读取文件,如果不存在则将其放入缓存中 + 对内容进行一些更改(例如,添加标签或属性):
public static class Foo
{
public static DoSomething(this HtmlHelper helper, string url, List<string> parameters)
{
string content = String.Empty;
var cache = HttpContext.Current.Cache[url];
if (cache == null)
{
string absolute_path = WebPages.HelperPage.Server.MapPath(url);
content = File.ReadAllText(absolute_path);
HttpContext.Current.Cache.Add(url, content, ...);
}
else
{
content = cache.ToString();
}
//make a few changes to content (e.g., add some tags or attributes)
content = makeChangesToContent(content, parameters);
return MvcHtmlString.Create(content);
}
}
此方法在剃刀视图中使用如下:
@Html.DoSomething("/path/to/file", new List<string>(){"param1", "param2"});
为了使这段代码可测试,我必须从方法中删除所有依赖项。但由于它是静态的并且在剃刀视图中使用,所以我不确定如何正确操作。
我想到的唯一一个选择是使用 Shims 通过一些伪造的方法来伪造外部依赖项。但是单元测试代码看起来有点重,运行大约需要200ms。
这是单元测试的一个简单示例:
[Test]
public void DoSomething_Should_Return_FileContent_From_Cache_When_It_Is_There()
{
string relativeFilePath = "/some/path";
string fileContent = "content";
string cachedKey = String.Empty;
object cachedValue = null;
using (ShimsContext.Create())
{
//Arrange
System.Web.Fakes.ShimHttpContext.CurrentGet = () =>
{
var httpContext = new System.Web.Fakes.ShimHttpContext();
httpContext.CacheGet = () =>
{
var cache = new System.Web.Caching.Fakes.ShimCache();
cache.ItemGetString = (key) =>
{
cachedKey = key;
cachedValue = fileContent;
return fileContent;
};
return cache;
};
return httpContext;
};
//Act
var result = helper.DoSomething(relativeFilePath, new List<string>(){"param1", "param2"});
//Assert
Assert.IsTrue(cachedKey.Equals(relativeFilePath));
Assert.IsTrue(cachedValue.Equals(fileContent));
}
测试方法是否正确?有没有更好的选择? 根据您的经验,测试此类静态方法的最佳方法是什么?
【问题讨论】:
-
这可能会有所帮助 - stackoverflow.com/questions/4379450/…
-
您使用的 Visual Studio 版本是什么?
标签: c# asp.net-mvc unit-testing razor static-methods