httpcontext

Setting HttpContext.Current.Session in a unit test

天涯浪子 提交于 2019-11-26 01:44:04
问题 I have a web service I am trying to unit test. In the service it pulls several values from the HttpContext like so: m_password = (string)HttpContext.Current.Session[\"CustomerId\"]; m_userID = (string)HttpContext.Current.Session[\"CustomerUrl\"]; in the unit test I am creating the context using a simple worker request, like so: SimpleWorkerRequest request = new SimpleWorkerRequest(\"\", \"\", \"\", null, new StringWriter()); HttpContext context = new HttpContext(request); HttpContext.Current

How do I mock the HttpContext in ASP.NET MVC using Moq?

≡放荡痞女 提交于 2019-11-26 00:37:49
问题 [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). ... } 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

Mock HttpContext.Current in Test Init Method

独自空忆成欢 提交于 2019-11-26 00:32:12
问题 I\'m trying to add unit testing to an ASP.NET MVC application I have built. In my unit tests I use the following code: [TestMethod] public void IndexAction_Should_Return_View() { var controller = new MembershipController(); controller.SetFakeControllerContext(\"TestUser\"); ... } With the following helpers to mock the controller context: public static class FakeControllerContext { public static HttpContextBase FakeHttpContext(string username) { var context = new Mock<HttpContextBase>();