Click here to Skip to main content
15,880,956 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.8K   572   11  
This article explains how to write unit tests for MVVM using Catel.
using System;
using Catel.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Catel.Test.Reflection
{
	[TestClass]
	public class PropertyHelperTest
	{
        #region Test classes
        public class MyPropertyHelperClass
        {
            private int _privateReadField;
            private int _privateWriteField;

            public MyPropertyHelperClass()
            {
                PublicProperty = 1;
                _privateReadField = 2;
                _privateWriteField = 4;
            }

            public int PublicProperty { get; set; }

            public int PrivateReadProperty { set { _privateReadField = value; } }

            public int PrivateWriteProperty { get { return _privateWriteField; } }

            public string StringValue { get; set; }

            public int GetPrivateReadPropertyValue()
            {
                return _privateReadField;
            }
        }
        #endregion

        #region GetPropertyValue
        [TestMethod]
        public void GetPropertyValue_NullInput()
        {
            try
            {
                PropertyHelper.GetPropertyValue(null, "PublicProperty");
                Assert.Fail("Expected ArgumentNullException");
            }
            catch (ArgumentNullException ex)
            {
#if !SILVERLIGHT
                Assert.AreEqual("obj", ex.ParamName); 
#endif
            }
        }

        [TestMethod]
        public void GetPropertyValue_NotExistingProperty()
        {
            try
            {
                MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
                PropertyHelper.GetPropertyValue(myPropertyHelperClass, "NotExistingProperty");
                Assert.Fail("Expected PropertyNotFoundException");
            }
            catch (PropertyNotFoundException ex)
            {
                Assert.AreEqual("NotExistingProperty", ex.PropertyName);
            }
        }

        [TestMethod]
        public void GetPropertyValue_PrivateReadProperty()
        {
            try
            {
                MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
                PropertyHelper.GetPropertyValue(myPropertyHelperClass, "PrivateReadProperty");
                Assert.Fail("Expected CannotGetPropertyValueException");
            }
            catch (CannotGetPropertyValueException ex)
            {
                Assert.AreEqual("PrivateReadProperty", ex.PropertyName);
            }
        }

        [TestMethod]
        public void GetPropertyValue_PrivateWriteProperty()
        {
            MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
            Assert.AreEqual(4, PropertyHelper.GetPropertyValue(myPropertyHelperClass, "PrivateWriteProperty"));
        }

        [TestMethod]
        public void GetPropertyValue_PublicProperty()
        {
            MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
            myPropertyHelperClass.PublicProperty = 42;
            Assert.AreEqual(42, PropertyHelper.GetPropertyValue(myPropertyHelperClass, "PublicProperty"));            
        }

        [TestMethod]
        public void GetPropertyValue_StringValue()
        {
            MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
            myPropertyHelperClass.StringValue = "FourtyTwo";
            Assert.AreEqual("FourtyTwo", PropertyHelper.GetPropertyValue(myPropertyHelperClass, "StringValue"));
        }
        #endregion

        #region SetPropertyValue
        [TestMethod]
        public void SetPropertyValue_NullInput()
        {
            try
            {
                PropertyHelper.SetPropertyValue(null, "PublicProperty", 42);
                Assert.Fail("Expected ArgumentNullException");
            }
            catch (ArgumentNullException ex)
            {
#if !SILVERLIGHT
                Assert.AreEqual("obj", ex.ParamName);
#endif
            }
        }

        [TestMethod]
        public void SetPropertyValue_NotExistingProperty()
        {
            try
            {
                MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
                PropertyHelper.SetPropertyValue(myPropertyHelperClass, "NotExistingProperty", 42);
                Assert.Fail("Expected PropertyNotFoundException");
            }
            catch (PropertyNotFoundException ex)
            {
                Assert.AreEqual("NotExistingProperty", ex.PropertyName);
            }
        }

        [TestMethod]
        public void SetPropertyValue_PrivateReadProperty()
        {
            MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
            PropertyHelper.SetPropertyValue(myPropertyHelperClass, "PrivateReadProperty", 42);
            Assert.AreEqual(42, myPropertyHelperClass.GetPrivateReadPropertyValue());
        }

        [TestMethod]
        public void SetPropertyValue_PrivateWriteProperty()
        {
            try
            {
                MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
                PropertyHelper.SetPropertyValue(myPropertyHelperClass, "PrivateWriteProperty", 42);
                Assert.Fail("Expected CannotSetPropertyValueException");
            }
            catch (CannotSetPropertyValueException ex)
            {
                Assert.AreEqual("PrivateWriteProperty", ex.PropertyName);
            }
        }

        [TestMethod]
        public void SetPropertyValue_PublicProperty()
        {
            MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
            PropertyHelper.SetPropertyValue(myPropertyHelperClass, "PublicProperty", 42);
            Assert.AreEqual(42, myPropertyHelperClass.PublicProperty);
        }

        [TestMethod]
        public void SetPropertyValue_StringValue()
        {
            MyPropertyHelperClass myPropertyHelperClass = new MyPropertyHelperClass();
            PropertyHelper.SetPropertyValue(myPropertyHelperClass, "StringValue", "FourtyTwo");
            Assert.AreEqual("FourtyTwo", myPropertyHelperClass.StringValue);
        }

        #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