ASP.NET MVC does not add ModelError when invoking from unit test

Posted by Tomas Lycken on Stack Overflow See other posts from Stack Overflow or by Tomas Lycken
Published on 2010-04-06T02:55:39Z Indexed on 2010/04/06 3:03 UTC
Read the original article Hit count: 317

I have a model item

public class EntryInputModel
{
    ...

    [Required(ErrorMessage = "Description is required.", AllowEmptyStrings = false)]
    public virtual string Description { get; set; }
}

and a controller action

public ActionResult Add([Bind(Exclude = "Id")] EntryInputModel newEntry)
{
    if (ModelState.IsValid)
    {
        var entry = Mapper.Map<EntryInputModel, Entry>(newEntry);

        repository.Add(entry);
        unitOfWork.SaveChanges();
        return RedirectToAction("Details", new { id = entry.Id });
    }
    return RedirectToAction("Create");
}

When I create an EntryInputModel in a unit test, set the Description property to null and pass it to the action method, I still get ModelState.IsValid == true, even though I have debugged and verified that newEntry.Description == null.

Why doesn't this work?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about model-binding