本文介绍了我如何使用模拟起订量在ASP.NET MVC中的HttpContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[TestMethod]
public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist()
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
context
.Setup(c => c.Request)
.Returns(request.Object);
HomeController controller = new HomeController();
controller.HttpContext = context; //Here I am getting an error (read only).
...
}
我的基本控制器具有初始化拿到的这款的RequestContext的overrride。我想一起传递,但我没有做正确的事情。
my base controller has an overrride of the Initialize that get's this requestContext. I am trying to pass this along but I am not doing something right.
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
}
我在哪里可以得到嘲笑我的RequestContext和使用HttpContext的起订量的更多信息?我试图模仿饼干和一般的背景。
Where can I get more information on mocking my RequestContext and HttpContext using Moq? I am trying to mock cookies and the general context.
推荐答案
HttpContext的是只读的,但它实际上是从ControllerContext,你可以设置的。
HttpContext is read-only, but it is actually derived from the ControllerContext, which you can set.
controller.ControllerContext = new ControllerContext( context.Object, new RouteData(), controller );
这篇关于我如何使用模拟起订量在ASP.NET MVC中的HttpContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!