Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 417211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:33:28+00:00 2026-05-12T18:33:28+00:00

I am reading Asp.net MVC Framework and I am reading about IDataErrorInfo as form

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:33:29+00:00Added an answer on May 12, 2026 at 6:33 pm

    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 */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a post about mobile web development and ASP.NET MVC here: http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx
I'm reading ScottGu's blog about ASP.NET MVC, and found a lot of code cannot
I've been reading a few things about ASP.NET MVC, SOLID and so on, and
I have finally finished reading Pro ASP.NET MVC 2 Framework Chapter 3: Pre-requisites but
I've been reading a lot about .NET's step into the MVC framework. I've tried
While reading an Asp.Net MVC code sample that used MbUnit as it's testing framework,
I'm reading Steven Sanderson's book Pro ASP.NET MVC 2 Framework . In his book,
I am reading Pro ASP.NET MVC 2 Framework by Steven Sanderson (Apress) and I
I been reading pro asp.net mvc 2.0 framework and I am a bit confused
I'm reading Apress Pro ASP.NET MVC Framework and there are some LINQ to SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.