【发布时间】:2016-03-01 07:23:01
【问题描述】:
public class DemoController : Controller
{
private readonly ICommonOperationsRepository _commonRepo;
public DemoController (ICommonOperationsRepository commonRepo)
{
_commonRepo = commonRepo;
}
public ActionResult Default()
{
var model = new DemoModel();
try
{
**DeviceDetection dd = new DeviceDetection(Request.ServerVariables["HTTP_X_REWRITE_URL"].ToString());
dd.DetectDevice();**
model.ListTopListing.AddRange(_commonRepo.GetListings());
}
catch (Exception ex)
{
ExceptionHandler objErr = new ExceptionHandler(ex, "DemoController .Default()\n Exception : " + ex.Message);
objErr.LogException();
}
return View(model);
}
}
问题:DeviceDetection 在这里有具体的依赖关系,所以我不能对我的控制器进行单元测试。我不想模拟 Http 请求,因为我只想测试控制器而不是 DeviceDetection 模块。
我如何模拟/避免访问这个(Request.ServerVariables["HTTP_X_REWRITE_URL"].ToString())
这是导致所有问题的原因。
【问题讨论】:
标签: asp.net-mvc unit-testing dependency-injection moq