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

ここwith_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);

値をに設定しないとresultresult明らかにになりますnull。はぁ。

于 2010-05-24T08:57:29.293 に答える