我使用 MVC3 并想通过Microsoft.VisualStudio.TestTools.UnitTesting
and测试以下操作Moq
:
public ActionResult index()
{
Session.Add("username", "Simon");
var lName = Session["username"] as String;
var lSessionID = Session.SessionID;
return Content(lSessionID);
}
我的单元测试:
[TestMethod]
public void IndexTest()
{
// Arrange
var contextMock = new Mock<ControllerContext>();
var mockHttpContext = new Mock<HttpContextBase>();
var session = new Mock<HttpSessionStateBase>();
mockHttpContext.Setup(ctx => ctx.Session).Returns(session.Object);
StartController controller = new StartController();
var lResult = controller.index() as ContentResult;
Assert......;
}
我的单元测试导致 NullReferenceException:
我也尝试过MvcContrib.TestHelper
失败,Session.SessionID
并显示“尚未实现”消息。
如何使用单元测试来测试我的操作?