场景:
我有一个基本 Controller ,它在 OnActionExecuting 覆盖中禁用缓存。
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); //IE
filterContext.HttpContext.Response.Cache.SetNoStore(); //FireFox
}
如何创建单元测试来测试此行为?
最佳答案
尝试做同样的事情。迄今为止我拥有的最好的就是这个......
[TestCase]
public void ResponseNotFromCache()
{
System.Net.WebRequest rq = System.Net.WebRequest.Create("testmethod");
System.Net.HttpWebResponse rs = rq.GetResponse() as System.Net.HttpWebResponse;
Assert.IsFalse(rs.IsFromCache);
}
一定有更好的方法!
关于asp.net-mvc - 帮助为测试响应 header 创建单元测试,特别是 Cache-Control,以确定缓存是否已禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2282337/