我有以下情况
if (_ldapAuthentication.IsAuthenticated (Domain, login.username, login.PassWord))
{
_ldapAuthentication.LogOn (indexViewModel.UserName, false);
_authenticationService.LimpaTentativas (login.username);
return RedirectToAction ("Index", "Main");
}
Run Code Online (Sandbox Code Playgroud)
如果是真的,它会重定向到另一个页面..最好做一个测试?
Dan*_*Dan 17
在单元测试中,您只需对控制器返回的ActionResult断言.
//Arrange
var controller = new ControllerUnderTest(
mockLdap,
mockAuthentication
);
// Mock/stub your ldap behaviour here, setting up
// the correct return value for 'IsAuthenticated'.
//Act
RedirectResult redirectResult =
(RedirectResult) controller.ActionYouAreTesting();
//Assert
Assert.That( redirectResult.Url, Is.EqualTo("/Main/Index"));
Run Code Online (Sandbox Code Playgroud)
为了避免单元测试中可能出现的InvalidCastExceptions,这就是我经常做的事情:
//Arrange
var controller = new ControllerUnderTest(
mockLdap,
mockAuthentication
);
// Mock your ldap behaviour here, setting up
// the correct return value for 'IsAuthenticated'.
//Act
var result = controller.ActionYouAreTesting() as RedirectToRouteResult;
// Assert
Assert.NotNull(result, "Not a redirect result");
Assert.IsFalse(result.Permanent); // Or IsTrue if you use RedirectToActionPermanent
Assert.AreEqual("Index", result.RouteValues["Action"]);
Assert.AreEqual("Main", result.RouteValues["Controller"]);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6654 次 |
最近记录: |