2014-05-25 3 views
1

ASP.NET WebApi ApiControllers의 [단위 테스트를 위해] User.Identity를 조롱 할 수 있습니까?ASP.NET WebApi 컨트롤러의 User.Identity 조롱

참고 :

컨트롤러 방법의 반환 일부는 내가 MVC 컨트롤러 테스트를위한 비슷한 일을했다 다른 스레드

+0

도움이 될 수 있습니다 : http://stackoverflow.com/questions/162534/mock-iidentity-and-iprincipal – christiandev

+0

통합 테스트를 위해 메모리 내 HTTP 서버를 사용하는 것이 좋습니다. 파이프 라인에 위임 처리기를 삽입하여 인증을 처리 할 수 ​​있습니다. 이 [article] (http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/)이 도움이 될 수 있습니다. –

답변

1

에서 실행 aync [Task] 있습니다. 내가 한 일은 가짜 HttpContext를 컨트롤러에 연결 한 다음 GenericPrincipal을 가짜 컨텍스트에 연결하는 것입니다.

var myController = new SomeController(); 
var context = A.Fake<HttpContextBase>(); 
ControllerContext context = new ControllerContext(new RequestContext(context, new RouteData()), myController); 
myController.ControllerContext = context; 
myController.HttpContext.User = new GenericPrincipal(new GenericIdentity("bob"), null); 

가짜 생성을위한 구문이 다를 수 있으므로 FakeItEasy를 사용하고 있습니다.

+2

WebApi ApiCOntroller는 HttpControllerContext를 사용하며 HttpContext 속성을 가지고 있지 않습니다. – Tawani