断言重定向到操作

时间:2018-05-28 19:46:30

标签: asp.net-core fluent-assertions

使用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")这样的内容。

1 个答案:

答案 0 :(得分:1)

我个人会做以下事情:

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

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

        var result = controller.Index();

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

扩展方法+断言类

示例静态类

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));
    }

结果:

要重定向到“错误”操作的预期结果,但它会重定向到“索引”

N.B。

以上不测试控制器/区域差异或任何其他可能的组合,它只是查看动作名称。