Click here to Skip to main content
15,893,663 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.Windows.Media;
using System.Windows.Media.Imaging;
using Catel.Articles.Base.Data;
using Catel.Data;
using Catel.MVVM;

namespace Catel.Articles.Base.UI.ViewModels
{
    /// <summary>
    /// ExampleCode view model.
    /// </summary>
    public class ExampleViewModel : ViewModelBase
    {
        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="ExampleViewModel"/> class.
        /// </summary>
        /// <param name="example">The example info.</param>
        public ExampleViewModel(ExampleInfo example)
        {
            // Store values
            Example = example;

            // Create commands
            Run = new Command<object, object>(Run_Execute, Run_CanExecute);
        }
        #endregion

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

        #region Models
        /// <summary>
        /// Gets or sets the example info.
        /// </summary>
        [Model]
        private ExampleInfo Example
        {
            get { return GetValue<ExampleInfo>(ExampleProperty); }
            set { SetValue(ExampleProperty, value); }
        }

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

        #region View model
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [ViewModelToModel("Example")]
        public string Name
        {
            get { return GetValue<string>(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

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

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        [ViewModelToModel("Example", "LongDescription")]
        public string Description
        {
            get { return GetValue<string>(DescriptionProperty); }
            set { SetValue(DescriptionProperty, value); }
        }

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

        /// <summary>
        /// Gets or sets the code.
        /// </summary>
        [ViewModelToModel("Example")]
        public string Code
        {
            get { return GetValue<string>(CodeProperty); }
            set { SetValue(CodeProperty, value); }
        }

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

        /// <summary>
        /// Gets or sets the image source.
        /// </summary>
        public ImageSource ImageSource
        {
            get { return GetValue<ImageSource>(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

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

        #region Commands
        /// <summary>
        /// Gets the Run command.
        /// </summary>
        public Command<object, object> Run { get; private set; }

        /// <summary>
        /// Method to check whether the Run command can be executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private bool Run_CanExecute(object parameter)
        {
            return (Example.ClickHandler != null);
        }

        /// <summary>
        /// Method to invoke when the Run command is executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private void Run_Execute(object parameter)
        {
            Example.ClickHandler();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Initializes the object by setting default values.
        /// </summary>	
        protected override void Initialize()
        {
            // Load image
            if (Example.ImageUri != null)
            {
                // Load bitmapo
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = Example.ImageUri;
                bitmap.EndInit();

                // Set image source
                ImageSource = bitmap;
            }
            else
            {
                // Clear
                ImageSource = null;
            }
        }
        #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