Error using MVCContrib TestHelper

你说的曾经没有我的故事 提交于 2020-01-01 15:03:13

问题


While trying to implement the second answer to a previous question, I am receiving an error.

I have implemented the methods just as the post shows, and the first three work properly. The fourth one (HomeController_Delete_Action_Handler_Should_Redirect_If_Model_Successfully_Delete) gives this error: Could not find a parameter named 'controller' in the result's Values collection.

If I change the code to:

actual 
    .AssertActionRedirect() 
    .ToAction("Index");

it works properly, but I don't like the "magic string" in there and prefer to use the lambda method that the other poster used.

My controller method looks like this:

    [HttpPost]
    public ActionResult Delete(State model)
    {
        try
        {
            if( model == null )
            {
                return View( model );
            }

            _stateService.Delete( model );

            return RedirectToAction("Index");
        }
        catch
        {
            return View( model );
        }
    }

What am I doing wrong?


回答1:


MVCContrib.TestHelper expects you to specify the controller name when redirecting in the Delete action:

return RedirectToAction("Index", "Home");

Then you would be able to use the strongly typed assertion:

actual
    .AssertActionRedirect()
    .ToAction<HomeController>(c => c.Index());

Another alternative is to write your own ToActionCustom extension method:

public static class TestHelperExtensions
{
    public static RedirectToRouteResult ToActionCustom<TController>(
        this RedirectToRouteResult result, 
        Expression<Action<TController>> action
    ) where TController : IController
    {
        var body = (MethodCallExpression)action.Body;
        var name = body.Method.Name;
        return result.ToAction(name);
    }
}

which would allow you to leave the redirect as is:

return RedirectToAction("Index");

and test the result like this:

actual
    .AssertActionRedirect()
    .ToActionCustom<HomeController>(c => c.Index());


来源:https://stackoverflow.com/questions/2977580/error-using-mvccontrib-testhelper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!