views:

56

answers:

1

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?

+1  A: 

I found the answer. Instead of

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

I of course needed

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

since without setting the value to result, result will obviously be null. Sigh.

Tomas Lycken