views:

237

answers:

2

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.

+1  A: 

Unit testing IDataErrorInfo is quite easy. Just set up your tests with a "valid" instance of the object, then test that you can make it error:

[TestFixture]
public class ErrorTests
{
    private Product _product; // subject under test

    [SetUp]
    public void Create_valid_instance()
    {
        _product = new Product { /* valid values */ };
    }

    [Test]
    public void Name_cannot_be_null()
    {
        _product.Name = null; 
        Assert.AreEqual("Name is required.", _product.Error);
        Assert.AreEqual("Name is required.", _product["Name"]);
    }

    [Test]
    public void Name_cannot_be_empty()
    {
        _product.Name = String.Empty; 
        Assert.AreEqual("Name is required.", _product.Error);
        Assert.AreEqual("Name is required.", _product["Name"]);
    }

    [Test]
    public void Name_cannot_be_whitespace()
    {
        _product.Name = "   ";
        Assert.AreEqual("Name is required.", _product.Error);
        Assert.AreEqual("Name is required.", _product["Name"]);
    }

    /* etc - add tests to prove that errors can occur */
}
Matt Hamilton
This does not seem to be true. The error indexer (this[]) only appears to be called in my app when I turn on ValidatseOnDataErrors in my xaml. In my test app, which seems to lack a place to turn that on, the error indexer is never checked on my IDataErrorInfo business objects. So setting myObject.Name = string.Empty; will never check the name rule. I'm still looking for a way to do this.
Bob
A: 

The solution mentioned by Matt only works for me if i use the later statement to Assert -

Assert.AreEqual("Name is required.", _product["Name"]); - this works for me

Assert.AreEqual("Name is required.", _product.Error); - this doesn't work for me

Mihir