运行单元测试时出现以下错误:用户代码未处理 NullReferenceException。对象引用未设置为对象的实例。
不知道我哪里出错了。请指教。
我的 SessionManager.cs 类
public static int customerID
{
get
{
if (HttpContext.Current.Session != null &&
HttpContext.Current.Session[_customerID] != null)
{
return Convert.ToInt32(HttpContext.Current.Session[_customerID]);
}
else
{
throw new Ramsell.ExceptionManagement.RamsellException();
}
}
set
{
if (HttpContext.Current.Session == null) return;
HttpContext.Current.Session[_customerID] = value;
}
}
参与者控制器
public ActionResult Index()
{
int custId = Convert.ToInt32(SessionManager.customerID);
//code logic about participant lists
return View();
}
测试方法 :
[TestMethod]
public void IndexTest()
{
ParticipantController pCTarget = new ParticipantController();
const int page = 1;
const string sortBy = "FirstName";
const bool ascending = true;
const string partname = "";
const int success = -1;
const string id = "";
var expected = typeof(ParticipantListViewModel);
ViewResult result = pCTarget.Index(page, sortBy, ascending, partname, success, id) as ViewResult;
Assert.AreEqual(expected, result.Model.GetType());
}
//这是我的模拟类
public Mock<RequestContext> RoutingRequestContext { get; private set; }
public Mock<HttpContextBase> Http { get; private set; }
public Mock<HttpServerUtilityBase> Server { get; private set; }
public Mock<HttpResponseBase> Response { get; private set; }
public Mock<HttpRequestBase> Request { get; private set; }
public Mock<HttpSessionStateBase> Session { get; private set; }
public Mock<ActionExecutingContext> ActionExecuting { get; private set; }
public HttpCookieCollection Cookies { get; private set; }
public MockContext()
{
this.RoutingRequestContext = new Mock<RequestContext>(MockBehavior.Loose);
this.ActionExecuting = new Mock<ActionExecutingContext>(MockBehavior.Loose);
this.Http = new Mock<HttpContextBase>(MockBehavior.Loose);
this.Server = new Mock<HttpServerUtilityBase>(MockBehavior.Loose);
this.Response = new Mock<HttpResponseBase>(MockBehavior.Loose);
this.Request = new Mock<HttpRequestBase>(MockBehavior.Loose);
this.Session = new Mock<HttpSessionStateBase>(MockBehavior.Loose);
this.Cookies = new HttpCookieCollection();
this.RoutingRequestContext.SetupGet(c => c.HttpContext).Returns(this.Http.Object);
this.ActionExecuting.SetupGet(c => c.HttpContext).Returns(this.Http.Object);
this.Http.SetupGet(c => c.Request).Returns(this.Request.Object);
this.Http.SetupGet(c => c.Response).Returns(this.Response.Object);
this.Http.SetupGet(c => c.Server).Returns(this.Server.Object);
this.Http.SetupGet(c => c.Session).Returns(this.Session.Object);
this.Request.Setup(c => c.Cookies).Returns(Cookies);
var sessionContainer = new HttpSessionStateContainer("userID", new SessionStateItemCollection(),
new HttpStaticObjectsCollection(), 1, true,
HttpCookieMode.AutoDetect,
SessionStateMode.InProc, false);
this.Session.Setup(c => c.Add("AspSession", typeof(HttpSessionState).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null, CallingConventions.Standard,
new[] { typeof(HttpSessionStateContainer) },
null)
.Invoke(new object[] { sessionContainer })));
}