Why does this test fail?

Posted by Tomas Lycken on Stack Overflow See other posts from Stack Overflow or by Tomas Lycken
Published on 2010-05-24T06:39:08Z Indexed on 2010/05/24 6:41 UTC
Read the original article Hit count: 267

Filed under:
|
|

I'm trying to test/spec the following action method

public virtual ActionResult ChangePassword(ChangePasswordModel model)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
        {
            return RedirectToAction(MVC.Account.Actions.ChangePasswordSuccess);
        }
        else
        {
            ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
        }
    }
    // If we got this far, something failed, redisplay form
    return RedirectToAction(MVC.Account.Actions.ChangePassword);
}

with the following MSpec specification:

public class When_a_change_password_request_is_successful : with_a_change_password_input_model
{
    Establish context = () =>
    {
        membershipService.Setup(s => s.ChangePassword(Param.IsAny<string>(), Param.IsAny<string>(), Param.IsAny<string>())).Returns(true);
        controller.SetFakeControllerContext("POST");
    };

    Because of = () => controller.ChangePassword(inputModel);

    ThenIt should_be_a_redirect_result = () => result.ShouldBeARedirectToRoute();
    ThenIt should_redirect_to_success_page = () => result.ShouldBeARedirectToRoute().And().ShouldRedirectToAction<AccountController>(c => c.ChangePasswordSuccess());
}

where with_a_change_password_input_model is a base class that instantiates the input model, sets up a mock for the IMembershipService etc. The test fails on the first ThenIt (which is just an alias I'm using to avoid conflict with Moq...) with the following error description:

Machine.Specifications.SpecificationException: Should be of type System.RuntimeType but is [null]

But I am returning something - in fact, a RedirectToRouteResult - in each way the method can terminate! Why does MSpec believe the result to be null?

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc-2