Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 2 Validation Attribute Unit Testing Using IoC
    primarykey
    data
    text
    <p>I am currently writing some unit tests for an ASP.NET MVC 2 controller and having some issues with the MVC 2.0 validation attributes. The validation attribute in question uses a repository to check that no duplicate names exist in the current data source, and I would like to replace this repository with a fake one during my unit test. I am using Castle Windsor, as per <a href="http://www.dominicpettifer.co.uk/Blog/40/dependency-injection-in-asp-net-mvc-2---part-3--custom-dataannotation-validationattributes" rel="nofollow noreferrer">this article</a>, which allows me to replace the repository at run time, but am not sure how I can do this during the unit test.</p> <p>The bulk of the test code is this:</p> <pre><code>[TestMethod] public void TestSaveDuplicateTitleNameReturnsValidationErrorView() { //Arrange var titleRepository = new FakeTitleRepository(); var controller = new TitleController(titleRepository); var selectedTitle = new Title(); selectedTitle.Name = "Fru"; //Act var actual = controller.Save(selectedTitle); </code></pre> <p>A code sample from the controller is below:</p> <pre><code> public ActionResult Save(Title SelectedTitle) { if (TryValidateModel(SelectedTitle)) { TitleRepository.Add(SelectedTitle); TitleRepository.Save(); </code></pre> <p>The repository being used in the controller itself during the test is my fake one that I have passed, but when the TryValidateModel fires and the following code is run then it causes an exception as the repository is null:</p> <pre><code>[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public class ValidTitleNameAttribute : ValidationAttribute { public ITitleRepository Repository { get; set; } public override bool IsValid(object value) { bool isValid = true; PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value); object name = properties.Find("Name", true).GetValue(value); if (name == null) { return isValid = false; } var titles = Repository.GetAllTitles(); </code></pre> <p>Obviously in the repository property I could set a default instance to reference a concrete repository, which would prevent the exception during testing but that would defeat the purpose of using the IoC container. What would be the best way round this issue, would it be to find a way to mock the validation attribute in some way, or is there a way Windsor can inject the fake repository when the unit test runs? Any help/advice would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload