asp.net-mvc - 测试 RedirectToAction 的最佳方法是什么?

标签 asp.net-mvc

我有以下条件

if (_ldapAuthentication.IsAuthenticated (Domain, login.username, login.PassWord))
{
    _ldapAuthentication.LogOn (indexViewModel.UserName, false);
    _authenticationService.LimpaTentativas (login.username);
    return RedirectToAction ("Index", "Main");
}

是真的,它重定向到另一个页面.. 最好的测试是什么?

最佳答案

在单元测试中,您只需对 Controller 返回的 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"));

关于asp.net-mvc - 测试 RedirectToAction 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7601972/

相关文章:

asp.net-mvc - ASP.NET MVC UpdateModel 易受黑客攻击?

.net - 为什么 ValidateAntiForgeryTokenAttribute 允许匿名 token ?

c# - 在 Multi-Tenancy Web 应用程序中使用事件委托(delegate)

jquery - 如何将 ASP.NET MVC 3 View 显示为弹出窗口

asp.net-mvc - SessionSecurityTokenHandler 尝试使用 DPAPI 解密 RSA 加密的 cookie 中的 SessionSecurityToken;为什么?

asp.net-mvc - 使用压缩操作过滤器,服务器错误消息会清除内容编码 header 并呈现为乱码

javascript - 页面加载时,@Url.Action 为 null 或 javascript 中的 ""

asp.net - Application_Start 和 Application_OnStart 之间的区别

javascript - 使用js脚本后无法提交html表单

c# - 发布到 Azure 后找不到 View