搜索了StackOverflow,Google认为我在做的是假设是正确的,但是结果似乎并不顺利
[TestMethod] public void LoginAction_Should_Return_View_and_User_Authenticated() { // Arrange var mock = new Mock<ControllerContext>(); var mockSession = new Mock<HttpSessionStateBase>(); mock.Setup(p => p.HttpContext.Session).Returns(mockSession.Object); var testData = FakeUserData.CreateTestUsers(); var repository = new FakeUserRepository(testData); var controller = new AccountController(repository); controller.ControllerContext = mock.Object; // Act var result = controller.Login("testuser1","testuser1"); // Assert Assert.AreEqual("testuser1",controller.HttpContext.Session["Username"]); Assert.IsTrue((bool)controller.HttpContext.Session["IsAuthenticated"]); Assert.IsInstanceOfType(result,typeof(RedirectToRouteResult)); }
当我运行测试时,controller.HttpContext.Session [“Username”]的值为null,但是我使用Session帮助器将该值设置为用户名。我做的事情完全错了,还是别的什么?任何帮助将不胜感激。