Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 21st, 2009, 12:31 AM
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default How to unit test IDataErrorInfo?

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
Code:
    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.
Code:
    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
Code:
    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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unit Test josericardo_jr BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 0 August 9th, 2007 08:19 AM
Where are all the unit tests? Extrakun BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 1 July 17th, 2007 05:39 PM



All times are GMT -4. The time now is 05:39 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.