Click here to Skip to main content
15,881,559 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 Catel.Articles._04___Unit_testing.Models;
using Catel.Data;
using Catel.MVVM;

namespace Catel.Articles._04___Unit_testing.UI.ViewModels
{
    /// <summary>
    /// Person view model.
    /// </summary>
    public class PersonViewModel : ViewModelBase
    {
        #region Variables
        #endregion

        #region Constructor & destructor
		/// <summary>
		/// Initializes a new instance of the <see cref="PersonViewModel"/> class.
		/// </summary>
		/// <param name="person">The person.</param>
		public PersonViewModel(Person person)
		{
            Person = person ?? new Person();
		}
        #endregion

        #region Properties
        /// <summary>
        /// Gets the title of the view model.
        /// </summary>
        /// <value>The title.</value>
        public override string Title
        {
            get { return "Person"; }
		}

		#region Models
		/// <summary>
		/// Gets or sets the person model.
		/// </summary>
		[Model]
		public Person Person
		{
			get { return GetValue<Person>(PersonProperty); }
			private set { SetValue(PersonProperty, value); }
		}

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

		#region View model
		/// <summary>
		/// Gets or sets the gender.
		/// </summary>
		[ViewModelToModel("Person")]
		public Gender Gender
		{
			get { return GetValue<Gender>(GenderProperty); }
			set { SetValue(GenderProperty, value); }
		}

		/// <summary>
		/// Register the Gender property so it is known in the class.
		/// </summary>
		public static readonly PropertyData GenderProperty = RegisterProperty("Gender", typeof(Gender));

		/// <summary>
		/// Gets or sets the first name.
		/// </summary>
		[ViewModelToModel("Person")]
		public string FirstName
		{
			get { return GetValue<string>(FirstNameProperty); }
			set { SetValue(FirstNameProperty, value); }
		}

		/// <summary>
		/// Register the FirstName property so it is known in the class.
		/// </summary>
		public static readonly PropertyData FirstNameProperty = RegisterProperty("FirstName", typeof(string));

		/// <summary>
		/// Gets or sets the middle name.
		/// </summary>
		[ViewModelToModel("Person")]
		public string MiddleName
		{
			get { return GetValue<string>(MiddleNameProperty); }
			set { SetValue(MiddleNameProperty, value); }
		}

		/// <summary>
		/// Register the MiddleName property so it is known in the class.
		/// </summary>
		public static readonly PropertyData MiddleNameProperty = RegisterProperty("MiddleName", typeof(string));

		/// <summary>
		/// Gets or sets the last name.
		/// </summary>
		[ViewModelToModel("Person")]
		public string LastName
		{
			get { return GetValue<string>(LastNameProperty); }
			set { SetValue(LastNameProperty, value); }
		}

		/// <summary>
		/// Register the LastName property so it is known in the class.
		/// </summary>
		public static readonly PropertyData LastNameProperty = RegisterProperty("LastName", typeof(string));
		#endregion
		#endregion

		#region Methods
		#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