ASP.NET MVC 프레임 워크로 작성된 프로그램에 대해 Unit 테스트를 실행하려고하면 NullReferenceException이 발생합니다.HttpContextBaseExtensions.GetOwinContext는 Unit 테스트를 실행할 때 NullReferenceException을 발생시킵니다.
시험에 실패했습니다. System.NullReferenceException : 개체 참조 개체의 인스턴스로 설정되어 있지 않습니다. 나는
Logoff
방법을 실행하려고 할 때 System.Web.HttpContextBaseExtensions.GetOwinContext (HttpContextBase 컨텍스트)
에서이 오류가 발생합니다.
public ActionResult LogOff()
{
SessionWrapper.SetInSession("_Settings", null);
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
public class HttpContextSessionWrapper : ISessionWrapper
{
public T GetFromSession<T>(string key)
{
if (!string.IsNullOrEmpty(key))
return (T)HttpContext.Current.Session[key];
else
return default(T);
}
public void SetInSession(string key, object value)
{
if (!string.IsNullOrEmpty(key))
HttpContext.Current.Session[key] = value;
}
}
ASP.NET MVC에서 단위 테스트를 작성할 때이 NullReferenceException을 어떻게 수정합니까?
원래 질문이 너무 광범위했습니다. 나는 그것이 닫히지 않을 수 있도록 그것을 편집했다. 이 질문은 올바른 범위이지만 여전히 몇 가지 정보가 필요합니다. HttpContextBase를 어떻게 조롱하고 있습니까? 외부 클래스를 조롱하지 않으면 null이되고 NRE를 얻습니다. Rhino Mocks와 같은 조롱하는 프레임 워크에서 읽어야합니다. –
@GeorgeStocker 고마워요! –