如何测试ASP.NET MVC路由重定向到其他站点?

Mat*_*cey 6 asp.net-mvc routing unit-testing asp.net-mvc-routing

由于某些宣传材料中的原则错误,我有一个网站收到了很多请求,这些请求应该是一个网站到达另一个网站.

有效的网站是http://site1.com/abc&http://site2.com/def但人们被告知要去http://site1.com/def.

我控制了site1但不控制site2.

site1包含用于检查路由的第一部分在actionfilter中是否有效的逻辑,如下所示:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);

    if ((!filterContext.ActionParameters.ContainsKey("id")) 
     || (!manager.WhiteLabelExists(filterContext.ActionParameters["id"].ToString())))
    {
        if (filterContext.ActionParameters["id"].ToString().ToLowerInvariant().Equals("def"))
        {
            filterContext.HttpContext.Response.Redirect("http://site2.com/def", true);
        }

        filterContext.Result = new ViewResult { ViewName = "NoWhiteLabel" };
        filterContext.HttpContext.Response.Clear();
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何测试重定向到其他网站.
我已经有使用MvcContrib测试助手重定向到"NoWhiteLabel"的测试,但是这些情况无法处理(据我所见).

如何测试重定向到antoher站点?

Dar*_*rov 6

我建议你使用RedirectResult而不是调用Response.Redirect:

if (youWantToRedirect) 
{
    filterContext.Result = new RedirectResult("http://site2.com/def")
}
else
{
    filterContext.Result = new ViewResult { ViewName = "NoWhiteLabel" };
}
Run Code Online (Sandbox Code Playgroud)

现在,如果您知道如何ViewResult使用MVCContrib 进行测试,TestHelper您将能够以RedirectResult相同的方式进行测试.棘手的部分是嘲笑manager迫使它满足if条件.


更新:

以下是样本测试的外观:

    // arrange
    var mock = new MockRepository();
    var controller = mock.StrictMock<Controller>();
    new TestControllerBuilder().InitializeController(controller);
    var sut = new MyFilter();
    var aec = new ActionExecutingContext(
        controller.ControllerContext, 
        mock.StrictMock<ActionDescriptor>(), 
        new Dictionary<string, object>());

    // act
    sut.OnActionExecuting(aec);

    // assert
    aec.Result.ShouldBe<RedirectResult>("");
    var result = (RedirectResult)aec.Result;
    result.Url.ShouldEqual("http://site2.com/def", "");
Run Code Online (Sandbox Code Playgroud)

更新(由Matt Lacey提供)
以下是我实际工作的方式:

    // arrange
    var mock = new MockRepository();
    // Note that in the next line I create an actual instance of my real controller - couldn't get a mock to work correctly
    var controller = new HomeController(new Stubs.BlankContextInfoProvider(), new Stubs.BlankWhiteLabelManager());
    new TestControllerBuilder().InitializeController(controller);
    var sut = new UseBrandedViewModelAttribute(new Stubs.BlankWhiteLabelManager());

    var aec = new ActionExecutingContext(
        controller.ControllerContext,
        mock.StrictMock<ActionDescriptor>(),
        // being sure to specify the necessary action parameters
        new Dictionary<string, object> { { "id", "def" } });

    // act
    sut.OnActionExecuting(aec);

    // assert
    aec.Result.ShouldBe<RedirectResult>("");
    var result = (RedirectResult)aec.Result;
    result.Url.ShouldEqual("http://site2.com/def", "");
Run Code Online (Sandbox Code Playgroud)