我正在测试一个调用此类方法的MVC 3控制器:
public class SessionVar
{
/// <summary>
/// Gets the session.
/// </summary>
private static HttpSessionState Session
{
get
{
if (HttpContext.Current == null)
throw new ApplicationException
("No Http Context, No Session to Get!");
return HttpContext.Current.Session;
}
}
public static T Get<T>(string key)
{
return Session[key] == null ? default(T) : (T)Session[key];
}
...
}
我的测试方法,遵循Hanselman's Blog的建议:
[Test]
public void CanRenderEmployeeList()
{
_mockIEmployeeService.Setup(s => s.GetEmployees(StatusFilter.OnlyActive))
.Returns(BuildsEmployeeList().Where(e => e.IsApproved));
var httpContext = FakeHttpContext();
var target = _employeeController;
target.ControllerContext = new ControllerContext
(new RequestContext(httpContext, new RouteData()), target);
var result = target.Index();
Assert.IsNotNull(result);
Assert.IsInstanceOf<ViewResult>(result);
var viewModel = target.ViewData.Model;
Assert.IsInstanceOf<EmployeeListViewModel>(viewModel);
}
public static HttpContextBase FakeHttpContext()
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(session.Object);
context.Setup(ctx => ctx.Server).Returns(server.Object);
return context.Object;
}
但是我的测试仍然失败,我得到了:
CanRenderEmployeeListSystem.ApplicationException : No Http Context,
No Session to Get!
这是HttpContext.Current == null
我只需要将Session对象“存在”,而不是存储在Session上的实际值。
你能告诉我我做错了什么吗?
感谢。
答案 0 :(得分:2)
从长远来看,如果为SessionVar类创建接口,您会更高兴。在运行时使用当前实现(通过依赖注入)。在测试期间插入模拟。消除了模拟所有这些Http运行时依赖项的需要。
答案 1 :(得分:2)
您的Session属性中的HttpContext.Current不会受到您正在创建的模拟HttpContextBase的影响。也就是说,只是创建一个本地HttpContextBase不会自动填充HttpContext.Current。实际上HttpContext和HttpContextBase实际上并不相关。你必须使用HttpContextWrapper来统一它们。
因此,最好将HttpContextWrapper实现传递到类SessionVar中。在下面的代码中,我已将方法和属性更改为实例级别,以便我们可以在构造函数中设置上下文。如果没有向构造函数传递上下文,我们假设HttpContext.Current,但您也可以将您的模拟实例传递给您的测试。
public class SessionVar
{
HttpContextWrapper m_httpContext;
public SessionVar(HttpContextWrapper httpContext = null)
{
m_httpContext = httpContext ?? new HttpContextWrapper(HttpContext.Current);
}
/// <summary>
/// Gets the session.
/// </summary>
private HttpSessionState Session
{
get
{
if (m_httpContext == null)
throw new ApplicationException("No Http Context, No Session to Get!");
return m_httpContext.Session;
}
}
public T Get<T>(string key)
{
return Session[key] == null ? default(T) : (T)Session[key];
}
...
}