私は、次の資料に基づいて自分自身のモデルバインダーを作成している私のバリデーションを拡張する:私のアプリケーションで http://www.howmvcworks.net/OnModelsAndViewModels/TheBeautyThatIsTheModelBinderMVC変更ModelBinderユニットテスト
私はこのように私のPersonエンティティを拡張:
[たmetadataType( typeof演算(PersonMetaData))] 公共部分クラスPerson {}
パブリッククラスPersonMetaData { [CustomRegularExpression(@ "(\ワット|。)+ @(\ワット|。)+" は、ErrorMessageのは=「メールが無効です")] public string Name;私は私のPersonControllerのためのイベントを作成して呼び出し、提供電子メールが無効である、ModelState.Validフィールドがfalseの場合
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); //Change default modelbinding ModelBinders.Binders.DefaultBinder = new CustomModelBinder(); }
: }
私のGlobal.asaxのは、このようになります。私はModelState.Valid属性は誤りがないことを私に語っているコードをデバッグするとき
[TestInitialize()]
public void MyTestInitialize()
{
RegisterRoutes(RouteTable.Routes);
//Change default modelbinding
ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
}
/// <summary>
///A test for Create
///</summary>
// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
// http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
// whether you are testing a page, web service, or a WCF service.
[TestMethod()]
public void CreateTest()
{
PersonController controller = new PersonController();
Person Person = new Person();
Person.Email = "wrognmail.de
var validationContext = new ValidationContext(Person, null, null);
var validationResults = new List<ValidationResult>();
Validator.TryValidateObject(Person, validationContext, validationResults, true);
foreach (var validationResult in validationResults)
{
controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
}
ActionResult actual;
actual = controller.Create(Person);
// Make sure that our validation found the error!
Assert.IsTrue(controller.ViewData.ModelState.Count == 1, "err.");
}
:
今、私が作成する方法のためのユニットテストを作成したいです。私はDefaultBinderの登録が成功していないと思います。
単体テストにDefaultBinderを登録するにはどうすればよいですか?
ありがとうございました!