2

我正在尝试测试/指定以下操作方法

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

具有以下 MSpec 规范:

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

wherewith_a_change_password_input_model是一个基类,它实例化输入模型,为 IMembershipService 设置一个模拟等。第一个测试失败ThenIt(这只是我用来避免与 Moq 冲突的别名......),错误描述如下:

Machine.Specifications.SpecificationException:应为 System.RuntimeType 类型,但为 [null]

但我正在返回一些东西——事实上,一个RedirectToRouteResult——方法可以终止的每一种方式!为什么 MSpec 相信结果是null?

4

1 回答 1

2

我找到了答案。代替

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

我当然需要

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

因为没有将值设置为result,result显然会是null。叹。

于 2010-05-24T08:57:29.293 回答