次のアクションメソッドをテスト/仕様化しようとしています
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
ですか?