如何对IDataErrorInfo进行单元测试?

时间:2009-08-21 03:52:28

标签: asp.net-mvc unit-testing idataerrorinfo

我正在阅读Asp.net MVC框架,我正在阅读有关IDataErrorInfo作为验证的形式。

所以我只想发布他的内容。

产品类

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);
    }
}

控制器

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");
        }


    }
}

然而,在本书的哪个部分,我没有看到如何进行单元测试。就像他向你展示了如何对他的服务层进行单元测试,但没有关于单元测试IDataErrorInfo。

那我该如何对它进行单元测试呢?我喜欢检查错误消息以查看它们是否相同。就像我传入一个空字段一样,我想检查错误消息是否适合这个空字段。

我喜欢在需要验证的东西之后检查语句逻辑是否正在进行预期但我甚至不知道如何调用这个部分类,特别是因为你通常不想要在进行单元测试时点击数据库。

2 个答案:

答案 0 :(得分:2)

如果我将后面的语句用于Assert -

,Matt提到的解决方案只适用于我

Assert.AreEqual(“Name is required。”,_ product [“Name”]); - 这对我有用

Assert.AreEqual(“Name is required。”,_ product.Error); - 这对我不起作用

答案 1 :(得分:1)

单元测试IDataErrorInfo非常简单。只需使用对象的“有效”实例设置测试,然后测试是否可以使其出错:

[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 */
}