Hi
I am reading Asp.net MVC Framework and I am reading about IDataErrorInfo as form of validation.
So I am just going to post what he has.
Product Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace MvcApplication1.Models
{
public partial class Product : IDataErrorInfo
{
private Dictionary<string, string> _errors = new Dictionary<string, string>();
partial void OnNameChanging(string value)
{
if (value.Trim() == String.Empty)
_errors.Add("Name", "Name is required.");
}
partial void OnPriceChanging(decimal value)
{
if (value <= 0m)
_errors.Add("Price", "Price must be greater than 0.");
}
#region IDataErrorInfo Members
public string Error
{
get { return string.Empty; }
}
public string this[string columnName]
{
get
{
if (_errors.ContainsKey(columnName))
return _errors[columnName];
return string.Empty;
}
}
#endregion
}
}
ProductRepository.
using System.Collections.Generic;
using System.Linq;
namespace MvcApplication1.Models
{
public class ProductRepository : IProductRepository
{
private ProductsDBEntities _entities = new ProductsDBEntities();
public IEnumerable<Product> ListProducts()
{
return _entities.ProductSet.ToList();
}
public void CreateProduct(Product productToCreate)
{
_entities.AddToProductSet(productToCreate);
_entities.SaveChanges();
}
}
public interface IProductRepository
{
IEnumerable<Product> ListProducts();
void CreateProduct(Product productToCreate);
}
}
Controller
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class ProductController : Controller
{
private IProductRepository _repository;
public ProductController()
:this(new ProductRepository()){}
public ProductController(IProductRepository repository)
{
_repository = repository;
}
public ActionResult Index()
{
return View(_repository.ListProducts());
}
//
// GET: /Product/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Product/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude="Id")]Product productToCreate)
{
if (!ModelState.IsValid)
return View();
_repository.CreateProduct(productToCreate);
return RedirectToAction("Index");
}
}
}
Yet No where in the book do I see how to actually unit test this. Like he shows you how to unit test his service layer stuff but nothing about unit testing IDataErrorInfo.
So how would I unit test this? I like checking the error messages to see if they are the same. Like if I pass in a null field I like to check if the error message would be the right one for this null field.
After I like to check if statement logic after the stuff that needs to be validated to see if it is doing what is expected but I don't even know how to call this partial class up especially since you normally don't want to hit the database when doing unit tests.