Click here to Skip to main content
15,891,567 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 49.1K   572   11  
This article explains how to write unit tests for MVVM using Catel.
using System.Collections.ObjectModel;
using Catel.Data;

#if !SILVERLIGHT
using System;
using System.Runtime.Serialization;
#endif

namespace Catel.Examples.Models
{
    /// <summary>
    /// Family Data object class which fully supports serialization, property changed notifications,
    /// backwards compatibility and error checking.
    /// </summary>
#if !SILVERLIGHT
    [Serializable]
#endif
    public class Family : SavableDataObjectBase<Family>
    {
        #region Variables
        #endregion

        #region Constructor & destructor
        /// <summary>
        /// Initializes a new object from scratch.
        /// </summary>
        public Family()
        { }

#if !SILVERLIGHT
        /// <summary>
        /// Initializes a new object based on <see cref="SerializationInfo"/>.
        /// </summary>
        /// <param name="info"><see cref="SerializationInfo"/> that contains the information.</param>
        /// <param name="context"><see cref="StreamingContext"/>.</param>
        protected Family(SerializationInfo info, StreamingContext context)
            : base(info, context) { }
#endif
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the father.
        /// </summary>
        public Person Father
        {
            get { return GetValue<Person>(FatherProperty); }
            set { SetValue(FatherProperty, value); }
        }

        /// <summary>
        /// Register the property so it is known in the class.
        /// </summary>
        public readonly PropertyData FatherProperty = RegisterProperty("Father", typeof(Person), null);

        /// <summary>
        /// Gets or sets the mother.
        /// </summary>
        public Person Mother
        {
            get { return GetValue<Person>(MotherProperty); }
            set { SetValue(MotherProperty, value); }
        }

        /// <summary>
        /// Register the property so it is known in the class.
        /// </summary>
        public readonly PropertyData MotherProperty = RegisterProperty("Mother", typeof(Person), null);

        /// <summary>
        /// Gets or sets the children.
        /// </summary>
        public ObservableCollection<Person> Children
        {
            get { return GetValue<ObservableCollection<Person>>(ChildrenProperty); }
            set { SetValue(ChildrenProperty, value); }
        }

        /// <summary>
        /// Register the property so it is known in the class.
        /// </summary>
        public readonly PropertyData ChildrenProperty = RegisterProperty("Children", typeof(ObservableCollection<Person>), new ObservableCollection<Person>());
        #endregion

        #region Methods
        /// <summary>
        /// Validates the fields.
        /// </summary>
        protected override void ValidateFields()
        {
            if (Father == null)
            {
                SetFieldWarning(FatherProperty, "No father in this family");
            }

            if (Mother == null)
            {
                SetFieldWarning(MotherProperty, "No mother in this family");
            }
        }

        /// <summary>
        /// Validates the business rules.
        /// </summary>
        protected override void ValidateBusinessRules()
        {
            // No business rules yet
        }
        #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