Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 1.69 KB  |  hits: 33  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. MVC change ModelBinder unit test
  2. protected void Application_Start()
  3.     {
  4.         AreaRegistration.RegisterAllAreas();
  5.  
  6.         RegisterGlobalFilters(GlobalFilters.Filters);
  7.         RegisterRoutes(RouteTable.Routes);
  8.  
  9.         //Change default modelbinding
  10.         ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
  11.  
  12.     }
  13.        
  14. [TestInitialize()]
  15.     public void MyTestInitialize()
  16.     {
  17.  
  18.         RegisterRoutes(RouteTable.Routes);
  19.  
  20.         //Change default modelbinding
  21.         ModelBinders.Binders.DefaultBinder = new CustomModelBinder();
  22.  
  23.     }
  24.     /// <summary>
  25.     ///A test for Create
  26.     ///</summary>
  27.     // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
  28.     // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server,
  29.     // whether you are testing a page, web service, or a WCF service.
  30.     [TestMethod()]
  31.     public void CreateTest()
  32.     {
  33.  
  34.         PersonController controller = new PersonController();
  35.         Person Person = new Person();
  36.  
  37.         Person.Email = "wrognmail.de
  38.  
  39.             var validationContext = new ValidationContext(Person, null, null);
  40.         var validationResults = new List<ValidationResult>();
  41.         Validator.TryValidateObject(Person, validationContext, validationResults, true);
  42.         foreach (var validationResult in validationResults)
  43.         {
  44.             controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
  45.         }
  46.  
  47.         ActionResult actual;
  48.         actual = controller.Create(Person);
  49.  
  50.         // Make sure that our validation found the error!
  51.         Assert.IsTrue(controller.ViewData.ModelState.Count == 1, "err.");
  52.     }