Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 48.9K   572   11  
This article explains how to write unit tests for MVVM using Catel.
using System.Linq;
using Catel.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Catel.Test.Data
{
    [TestClass]
    public class PropertyDataManagerTest
    {
        [TestMethod]
        public void GetProperties()
        {
            PropertyDataManager propertyDataManager = new PropertyDataManager();

            RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");
            RegisterProperty(propertyDataManager, "objectProperty", new object());
            RegisterProperty(propertyDataManager, "intProperty", 1);

            var registeredProperties = propertyDataManager.GetProperties(typeof(PropertyDataManagerTest));
            var keys = registeredProperties.Keys.ToArray();
            Assert.AreEqual(3, registeredProperties.Count);
            Assert.AreEqual("stringProperty", keys[0]);
            Assert.AreEqual("objectProperty", keys[1]);
            Assert.AreEqual("intProperty", keys[2]);
        }

        [TestMethod]
        public void RegisterProperty_SinglePropertyRegistration()
        {
            PropertyDataManager propertyDataManager = new PropertyDataManager();

            RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");

            var registeredProperties = propertyDataManager.GetProperties(typeof(PropertyDataManagerTest));
            Assert.AreEqual(1, registeredProperties.Count);
            Assert.AreEqual("defaultValue", registeredProperties["stringProperty"].GetDefaultValue());
        }

        [TestMethod]
        [ExpectedException(typeof(PropertyAlreadyRegisteredException))]
        public void RegisterProperty_DoublePropertyRegistration()
        {
            PropertyDataManager propertyDataManager = new PropertyDataManager();

            RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");
            RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");
        }

        [TestMethod]
        public void IsPropertyRegistered_RegisteredProperty()
        {
            PropertyDataManager propertyDataManager = new PropertyDataManager();

            RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");

            Assert.IsTrue(propertyDataManager.IsPropertyRegistered(typeof(PropertyDataManagerTest), "stringProperty"));
        }

        [TestMethod]
        public void IsPropertyRegistered_UnregisteredProperty()
        {
            PropertyDataManager propertyDataManager = new PropertyDataManager();

            Assert.IsFalse(propertyDataManager.IsPropertyRegistered(typeof(PropertyDataManagerTest), "stringProperty"));
        }

        [TestMethod]
        public void GetPropertyData_RegisteredProperty()
        {
            PropertyDataManager propertyDataManager = new PropertyDataManager();

            RegisterProperty(propertyDataManager, "stringProperty", "defaultValue");

            var propertyData = propertyDataManager.GetPropertyData(typeof(PropertyDataManagerTest), "stringProperty");
            Assert.IsNotNull(propertyData);
            Assert.AreEqual("stringProperty", propertyData.Name);
        }

        [TestMethod]
        [ExpectedException(typeof(PropertyNotRegisteredException))]
        public void GetPropertyData_UnregisteredProperty()
        {
            PropertyDataManager propertyDataManager = new PropertyDataManager();

            var propertyData = propertyDataManager.GetPropertyData(typeof(PropertyDataManagerTest), "stringProperty");
        }

        [TestMethod]
        public void MapXmlNameToPropertyName_EqualPropertyAndXml()
        {
            ObjectWithXmlMappings objectWithXmlMappings = new ObjectWithXmlMappings();

            string propertyName = ObjectWithXmlMappings.PropertyDataManager.MapXmlNameToPropertyName(typeof(ObjectWithXmlMappings), "PropertyWithoutMapping");
            Assert.AreEqual("PropertyWithoutMapping", propertyName);
        }

        [TestMethod]
        public void MapXmlNameToPropertyName_DifferentPropertyAndXml()
        {
            ObjectWithXmlMappings objectWithXmlMappings = new ObjectWithXmlMappings();

            string propertyName = ObjectWithXmlMappings.PropertyDataManager.MapXmlNameToPropertyName(typeof(ObjectWithXmlMappings), "MappedXmlProperty");
            Assert.AreEqual("PropertyWithMapping", propertyName);            
        }

        [TestMethod]
        public void MapPropertyNameToXmlName_EqualPropertyAndXml()
        {
            ObjectWithXmlMappings objectWithXmlMappings = new ObjectWithXmlMappings();

            string xmlName = ObjectWithXmlMappings.PropertyDataManager.MapPropertyNameToXmlName(typeof(ObjectWithXmlMappings), "PropertyWithoutMapping");
            Assert.AreEqual("PropertyWithoutMapping", xmlName);
        }

        [TestMethod]
        public void MapPropertyNameToXmlName_DifferentPropertyAndXml()
        {
            ObjectWithXmlMappings objectWithXmlMappings = new ObjectWithXmlMappings();

            string xmlName = ObjectWithXmlMappings.PropertyDataManager.MapPropertyNameToXmlName(typeof(ObjectWithXmlMappings), "PropertyWithMapping");
            Assert.AreEqual("MappedXmlProperty", xmlName);    
        }

        #region Helper methods
        /// <summary>
        /// Registers the property with the property data manager. This is a shortcut method so the PropertyData doesn't have
        /// to be declared every time.
        /// </summary>
        /// <typeparam name="T">Type of the property.</typeparam>
        /// <param name="propertyDataManager">The property data manager.</param>
        /// <param name="name">The name.</param>
        /// <param name="defaultValue">The default value.</param>
        private static void RegisterProperty<T>(PropertyDataManager propertyDataManager, string name, T defaultValue)
        {
            propertyDataManager.RegisterProperty(typeof(PropertyDataManagerTest), name, new PropertyData(name, typeof(T), defaultValue, false, null, false, false, false));
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions