Click here to Skip to main content
15,886,007 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.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IViewModel.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   View model interface.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.ComponentModel;

namespace Catel.MVVM
{
	/// <summary>
	/// View model interface.
	/// </summary>
	public interface IViewModel : INotifyPropertyChanged
	{
		#region Properties
        /// <summary>
        /// Gets a value indicating whether the view model is initialized.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the view model is initialized; otherwise, <c>false</c>.
        /// </value>
	    bool IsInitialized { get; }

		/// <summary>
		/// Gets a value indicating whether this instance has errors.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance has errors; otherwise, <c>false</c>.
		/// </value>
		bool HasErrors { get; }

		/// <summary>
		/// Gets the title of the view model.
		/// </summary>
		/// <value>The title.</value>
		string Title { get; }
		#endregion

        #region Events
        /// <summary>
        /// Occurs when the view model is about the be saved.
        /// </summary>
        event EventHandler<EventArgs> Saving;

        /// <summary>
        /// Occurs when the view model is saved successfully.
        /// </summary>
        event EventHandler<EventArgs> Saved;

        /// <summary>
        /// Occurs when the view model is about to be canceled.
        /// </summary>
        event EventHandler<EventArgs> Canceling;

        /// <summary>
        /// Occurrs when the view model is canceled.
        /// </summary>
        event EventHandler<EventArgs> Canceled;

        /// <summary>
        /// Occurs when the view model is being closed.
        /// </summary>
        event EventHandler<EventArgs> Closed;
        #endregion

        #region Methods
        /// <summary>
		/// Initializes the data.
		/// </summary>
		void Initialize();

		/// <summary>
		/// Validates the data.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if validation succeeds; otherwise <c>false</c>.
		/// </returns>
		bool Validate();

		/// <summary>
		/// Validates the specified notify changed properties only.
		/// </summary>
		/// <param name="force">if set to <c>true</c>, a validation is forced (even if the object knows it is already validated).</param>
		/// <param name="notifyChangedPropertiesOnly">if set to <c>true</c> only the properties for which the warnings or errors have been changed
		/// will be updated via <see cref="INotifyPropertyChanged.PropertyChanged"/>; otherwise all the properties that
		/// had warnings or errors but not anymore and properties still containing warnings or errors will be updated.</param>
		/// <returns>
		/// 	<c>true</c> if validation succeeds; otherwise <c>false</c>.
		/// </returns>
		/// <remarks>
		/// This method is useful when the view model is initialized before the window, and therefore WPF does not update the errors and warnings.
		/// </remarks>
		bool Validate(bool force, bool notifyChangedPropertiesOnly);

		/// <summary>
		/// Cancels the editing of the data.
		/// </summary>
		void Cancel();

		/// <summary>
		/// Cancels the editing of the data, but also closes the view model in the same call.
		/// </summary>
		void CancelAndClose();

		/// <summary>
		/// Saves the data.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if successful; otherwise <c>false</c>.
		/// </returns>
		bool Save();

		/// <summary>
		/// Saves the data, but also closes the view model in the same call if the save succeeds.
		/// </summary>
		/// <returns>
		/// 	<c>true</c> if successful; otherwise <c>false</c>.
		/// </returns>
		bool SaveAndClose();

		/// <summary>
		/// Closes this instance. Always called after the <see cref="Cancel"/> of <see cref="Save"/> method.
		/// </summary>
		void Close();
		#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