Last active
November 6, 2023 22:05
-
-
Save johnnyreilly/5069901 to your computer and use it in GitHub Desktop.
Unit testing ModelState using Moq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
namespace MyApp | |
{ | |
public class CarController : Controller | |
{ | |
//... | |
public ActionResult Edit(CarModel model) | |
{ | |
if (ModelState.IsValid) { | |
//Save the model | |
return View("Details", model); | |
} | |
return View(model); | |
} | |
//... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel.DataAnnotations; | |
namespace MyNamespace.Model | |
{ | |
public class CarModel | |
{ | |
[Required, | |
Display(Name = "Purchased"), | |
DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] | |
public DateTime Purchased { get; set; } | |
[Required, | |
Display(Name = "Colour")] | |
public string Colour{ get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using Moq; | |
namespace UnitTests.TestUtilities | |
{ | |
/// <summary> | |
/// Instance of a controller for testing things that use controller methods i.e. controller.TryValidateModel(model) | |
/// </summary> | |
public class ModelStateTestController : Controller | |
{ | |
public ModelStateTestController() | |
{ | |
ControllerContext = (new Mock<ControllerContext>()).Object; | |
} | |
public bool TestTryValidateModel(object model) | |
{ | |
return TryValidateModel(model); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void Unit_Test_CarModel_ModelState_validations_are_thrown() | |
{ | |
// Arrange | |
var controller = new ModelStateTestController(); | |
var car = new CarModel | |
{ | |
Puchased = null, //This is a required property and so this value is invalid | |
Colour = null //This is a required property and so this value is invalid | |
}; | |
// Act | |
var result = controller.TestTryValidateModel(company); | |
// Assert | |
Assert.IsFalse(result); | |
var modelState = controller.ModelState; | |
Assert.AreEqual(2, modelState.Keys.Count); | |
Assert.IsTrue(modelState.Keys.Contains("Purchased")); | |
Assert.IsTrue(modelState["Purchased"].Errors.Count == 1); | |
Assert.AreEqual("The Purchased field is required.", modelState["Purchased"].Errors[0].ErrorMessage); | |
Assert.IsTrue(modelState.Keys.Contains("Colour")); | |
Assert.IsTrue(modelState["Colour"].Errors.Count == 1); | |
Assert.AreEqual("The Colour field is required.", modelState["Colour"].Errors[0].ErrorMessage); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var car = new CarModel | |
{ | |
Puchased = null, //This is a required property and so this value is invalid | |
Colour = null //This is a required property and so this value is invalid | |
}; |
where does "company" in var result = controller.TestTryValidateModel(company); come from?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not working in Web api test cases.