다음과 같이 사용자 객체를 보유하는 추상 보안 컨트롤러를 상속 한 컨트롤러가 있습니다. 내 API를 생성자가 너무생성자가 일관되게 호출되지 않았습니다.
같은 안전 컨트롤러의 기본 생성자를 호출해도public new User User
{
get
{
if (this.user == null)
{
var id = int.Parse(base.User.Identity.Name, CultureInfo.InvariantCulture);
this.user = this.UserRepository.FindById(id);
}
return this.user;
}
}
나는 다음과 같은 함수를 호출 할 때마다 나는 this.UserRepository
[UrlRoute(Path = "api/stats/events/visits/accounttype/{idList}")]
[UrlRoute(Path = "api/{idList}/stats/events/visits/accounttype")]
[UrlRouteParameterDefault(Name = "idList", Value = "")]
public virtual ActionResult Vsat(string idList, DateTime? startDate, DateTime? endDate)
{
// get the ids from the url and retrieve a list of events for those user/s
var ids = (from id in idList.Split(',') where !string.IsNullOrEmpty(id) select Convert.ToInt64(id)).ToList();
var allEvents = this.eventRepository.FindForCompanyBetweenDatesForUsers(
this.User.Company.Id, new List<EventType> { EventType.Visit }, startDate, endDate, ids).ToList();
var groupResults = allEvents.GroupBy(x => x.Account.AccountType.Name);
return null;
}
위의 널 (null) 예외가 발생 더 이상 무엇
public ApiController(IUserRepository userRepository) : base(userRepository)
{
}
protected SecureController(IUserRepository userRepository)
{
this.UserRepository = userRepository;
}
는 this.User를 참조하고 그들 중 누구도 널 (null) 같은 예외를 반환하지 페이지의 다른 기능이 있다는 것입니다. 그들은 보안 생성자를 누른 다음 API 생성자를 누른 다음 함수를 누릅니다. (테스트 목적으로 단지 이름) 위의 VSAT 함수는, 기능 안타 내가 그 위의 유사한 기능을 배치하면, 작동, 그 같은
this.user = this.UserRepository.FindById(id);
뿐만 아니라 라인에 중단하지만, 새로운 기능 다음 같은 문제가 있습니다.
편집 새로운 클래스를 생성
과 기능이 완벽하게 작동합니다.
public class TestController : SecureController
{
private readonly IEventRepository eventRepository;
public TestController(IUserRepository userRepository, IEventRepository eventRepository) : base(userRepository)
{
this.eventRepository = eventRepository;
}
[UrlRoute(Path = "test/stats/events/visits/accounttype/{idList}")]
[UrlRoute(Path = "test/{idList}/stats/events/visits/accounttype")]
[UrlRouteParameterDefault(Name = "idList", Value = "")]
public virtual ActionResult Vsat(string idList, DateTime? startDate, DateTime? endDate)
{
// get the ids from the url and retrieve a list of events for those user/s
var ids = (from id in idList.Split(',') where !string.IsNullOrEmpty(id) select Convert.ToInt64(id)).ToList();
var allEvents = this.eventRepository.FindForCompanyBetweenDatesForUsers(
this.User.Company.Id, new List<EventType> {EventType.Visit}, startDate, endDate, ids).ToList();
var groupResults = allEvents.GroupBy(x => x.Account.AccountType.Name);
return null;
}
}
문제를보다 간단하고 재현 가능한 테스트 케이스로 분리 할 수 있습니까? –
나는이 게시물을 업데이 트했다. 바라건대 그게 무슨 뜻인가? – JConstantine
@JLevett 이것은 특정 상황에서 null 참조가 주어진 기본 생성자의 단순한 사례 일 수 있다고 생각한다. 'UserRespository' 속성 setter에서 들어오는'value' ('if (value == null) throw new Exception ("null! why?");')을 테스트하고, null 일 경우 특정 예외를 throw하십시오. 확인. –