2009-08-21 122 views
-1

我正在阅读Asp.net MVC框架,我正在阅读有关IDataErrorInfo作为验证的形式。如何对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。

那么我会如何测试这个?我喜欢检查错误信息,看看它们是否相同。就像我传入一个空字段一样,我想检查错误消息是否是该空字段的正确字段。

之后我喜欢检查是否需要验证的东西之后的语句逻辑,看看它是否在做预期的工作,但我甚至不知道如何调用这个部分类,尤其是因为您通常不会希望在进行单元测试时碰到数据库。

+1

您发布的商品类是部分商品。必须有一个或多个Product类的其他实现(除其他外)调用部分方法。当我们不知道什么样子时,回答你的问题并不容易。另外,我没有看到Repository和Controller与这个有什么关系,所以也许你可以减少你的问题? – 2009-08-21 07:10:42

回答

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 */ 
} 
+1

这似乎不是真的。当我打开xaml中的ValidatseOnDataErrors时,错误索引器(this [])似乎只在我的应用程序中被调用。在我的测试应用程序中,似乎没有可以打开的地方,错误索引器从不在我的IDataErrorInfo业务对象上进行检查。所以设置myObject.Name = string.Empty;永远不会检查名称规则。我仍然在寻找一种方法来做到这一点。 – Bob 2010-02-26 14:35:20

+0

同意Bob。这并不完全正常... – CamronBute 2011-06-17 20:04:49

+1

所以,我们很清楚:我发布了一些测试,并且因为你不能让它们通过,所以你给我提供了低估?从错误属性以及索引器中返回错误!然后测试通过! – 2011-06-17 22:13:37

2

马特提到的解决方案只有当我使用后声明,断言对我的作品 -

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

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