断言重定向到操作

Asserting Redirect to Action

当使用 ASP.Net Core 2 MVC 和 FluentAssertions.AspNetCore.Mvc 时,如何断言控制器重定向到操作?

例如,给定以下控制器操作:

public IActionResult SomeAction() {
  return RedirectToAction("SomeOtherAction");
}

我将如何编写验证重定向的测试?

我正在寻找类似的东西:

[Fact]
public void SomeActionTest() {
  var controller = new SomeController();
  var result = controller.SomeAction();

  result.Should().BeRedirectedToRouteResult().WithAction("SomeOtherAction");
}

...除了 BeRedirectedToRouteResult().WithAction("SomeOtherAction"),我希望断言 BeRedirectedToAction("SomeOtherAction").

我个人会做如下事情:

创建一个包含扩展方法和断言 class 的 static class,它有一个名为 BeRedirectAction 的方法,然后可以像下面这样使用:

    [Fact]
    public void ActionReturnsView_ExpectedRedirectToError_TypeMismatch()
    {
        var controller = new HomeController();

        var result = controller.Index();

        result.Should().BeRedirectAction(nameof(HomeController.Error));
    }

扩展方法+断言Class

静态示例 class

public static class ActionResultAssertionExtensions
{
    public class ActionResultAssertions : ObjectAssertions
    {
        public new IActionResult Subject { get; }

        public ActionResultAssertions(IActionResult subject) : base(subject)
        {
            Subject = subject;
        }

        [CustomAssertion]
        public void BeRedirectAction(string actionName, string because= null, params object[] becauseArgs)
        {
            var redirectResult = AssertionExtensions.Should(Subject).BeOfType<RedirectToActionResult>().Which;

            var actual = redirectResult.ActionName;
            var expected = actionName;

            Execute.Assertion.ForCondition(actual == expected)
                .BecauseOf(because, becauseArgs)
                .FailWith("Expected {context} to redirect to {0} Action but it is redirecting to {1}", expected, actual);
        }
    }

    public static ActionResultAssertions Should(this IActionResult subject)
    {
        return new ActionResultAssertions(subject);
    }
}

样本测试

类型不匹配测试

这是当结果不是重定向时的失败示例:

    [Fact]
    public void ActionReturnsView_ExpectedRedirectToError_TypeMismatch()
    {
        var controller = new HomeController();

        var result = controller.Index();

        result.Should().BeRedirectAction(nameof(HomeController.Error));
    }

结果:

Expected type to be Microsoft.AspNetCore.Mvc.RedirectToActionResult, but found Microsoft.AspNetCore.Mvc.ViewResult.

成功测试

这是通过测试的示例

    [Fact]
    public void ActionRedirectsToError_ExpectedRedirectToError_TestShouldPass()
    {
        var controller = new HomeController();

        var result = controller.RedirectToError();

        result.Should().BeRedirectAction(nameof(HomeController.Error));
    }

不同动作测试

这是重定向到不同操作时测试失败的示例:

    [Fact]
    public void ActionRedirectsToIndex_ExpectedRedirectToError_TestSHouldFailSayingDifferentActionName()
    {
        var controller = new HomeController();

        var result = controller.RedirectToIndex();

        result.Should().BeRedirectAction(nameof(HomeController.Error));
    }

结果:

预期结果重定向到 "Error" 操作,但它正在重定向到 "Index"

N.B.

以上不测试 controller/area 差异或任何其他可能的组合,它只查看操作名称。