【发布时间】:2010-09-10 20:35:58
【问题描述】:
我正在使用 MvcContrib 和 TestControllerBuilder 为我的控制器编写测试。
我正在为我的错误处理控制器编写测试,如下所示:
public JsonResult HttpError()
{
Exception ex = null;
try
{
ex = (Exception)HttpContext.Application[Request.UserHostAddress.ToString()];
}
catch
{
}
if( ex != null )
{
return Json( new ErrorViewModel() { Message = ex.Message, Source = ex.Source, StackTrace = ex.StackTrace, Type = ex.GetType().Name}, JsonRequestBehavior.AllowGet );
}
else
{
return Json( new ErrorViewModel() { Message = "An error has occured." }, JsonRequestBehavior.AllowGet );
}
}
基本上,我的全局错误处理将最后一个异常放入应用程序存储中,并且此控制器尝试将其拉回,将其转换为 Json,然后返回它(我们将所有内容作为 Json 返回,因为这些方法仅被调用为网络服务)。
要对此进行全面测试,我需要 UserHostAddress 包含可预测的内容,但 TestControllerBuilder 设置的对象将该属性保留为空。
我怎样才能做到这一点?我不确定如何在我的测试中完成这项工作。
【问题讨论】:
标签: asp.net-mvc nunit mocking mvccontrib