난 당신이 코딩 무엇을했던,하지만 난 여전히 도달 할 때 사용자가 null 얻을 : 내가 가지고있는
mockMembershipService.Setup(cp => cp.ChangePassword("johndoe", currentPassword, newPassword)).Returns(true);
을 내 TestClass에에서 :
이
//Arrange (Set up a scenario)
var mockMembershipService = new Mock<IMembershipService>();
MockIdentity identity = new MockIdentity("JohnDoe");
var httpContext = MoqHelpers.GetHttpContext(new MockPrincipal(identity, null));
var controller = new AccountController(null, mockMembershipService.Object, null, null, null);
string currentPassword = "qwerty";
string newPassword = "123456";
string confirmPassword = "123456";
// Expectations
mockMembershipService.Setup(pw => pw.MinPasswordLength).Returns(6);
mockMembershipService.Setup(cp => cp.ChangePassword("johndoe", currentPassword, newPassword)).Returns(true);
내가 내 cp.ChangePassword를 호출 함 잘못된 매개 변수가 있습니까? 그리고 MVCContrib Testhelpers 클래스가 Http 컨텍스트를 모의 할 수 있어야합니까? MVCContrib를 사용하여 User.Identity.Name을 설정하는 방법에 대한 정보를 찾을 수 없습니다.나는 (모의) 세션 뭔가를 테스트하기 위해 튜토리얼에서 이것을 사용했다 :
는
var builder = new TestControllerBuilder();
var controller = new AccountController(mockFormsAuthentication.Object, mockMembershipService.Object, mockUserRepository.Object, null, mockBandRepository.Object);
builder.InitializeController(controller);
는 편집 : 나는 조금 더를 온 :
MockIdentity identity = new MockIdentity("JohnDoe");
var httpContext = MoqHelpers.GetHttpContext(new MockPrincipal(identity, null));
var controller = new AccountController(null, mockMembershipService.Object, null, null, null);
controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);
을하지만 내 지금은 얻을 수없는 내 기대에 cp.ChangePassword는 true를 반환합니다 : 나는 "JohnDoe에"문자열을 보내고
mockMembershipService.Setup(cp => cp.ChangePassword("johndoe", currentPassword, newPassword)).Returns(true);
, 그것은을위한 매개 변수로 문자열을 필요로하기 때문에 User.Identity.Name,하지만 true를 반환하지 않습니다.
Mo 응답을 업데이트하여 Moq을 사용하여 HttpContext를 설정하고 모의 HttpPrincipal 및 ID를 통합하는 방법을 확인했습니다. – Razzie