Click here to Skip to main content
15,892,298 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.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MessageService.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Test implementation of the <see cref="IMessageService"/>.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Catel.Windows.Properties;

namespace Catel.MVVM.Services.Test
{
    /// <summary>
    /// Test implementation of the <see cref="IMessageService"/>.
    /// </summary>
    /// <example>
    /// <code>
    /// <![CDATA[
    /// 
    /// Test.MessageService service = (Test.MessageService)GetService<IMessageService>();
    /// 
    /// // Queue the next expected result
    /// service.ExpectedResults.Add(MessageResult.Yes);
    /// 
    /// ]]>
    /// </code>
    /// </example>
    public class MessageService : IMessageService
    {
        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="MessageService"/> class.
        /// </summary>
        public MessageService()
        {
            ExpectedResults = new Queue<MessageResult>();
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the queue of expected results.
        /// </summary>
        /// <value>The expected results.</value>
        public Queue<MessageResult> ExpectedResults { get; private set; }
        #endregion

        #region Methods
        /// <summary>
        /// Shows the error.
        /// </summary>
        /// <param name="exception">The exception.</param>
        public void ShowError(Exception exception)
        {
            // No implementation by design
        }

        /// <summary>
        /// Shows the error.
        /// </summary>
        /// <param name="message">The message.</param>
        public void ShowError(string message)
        {
            // No implementation by design
        }

        /// <summary>
        /// Shows the error.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="caption">The caption.</param>
        public void ShowError(string message, string caption)
        {
            // No implementation by design
        }

        /// <summary>
        /// Shows the warning.
        /// </summary>
        /// <param name="message">The message.</param>
        public void ShowWarning(string message)
        {
            // No implementation by design
        }

        /// <summary>
        /// Shows the warning.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="caption">The caption.</param>
        public void ShowWarning(string message, string caption)
        {
            // No implementation by design
        }

        /// <summary>
        /// Shows the information.
        /// </summary>
        /// <param name="message">The message.</param>
        public void ShowInformation(string message)
        {
            // No implementation by design
        }

        /// <summary>
        /// Shows the information.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="caption">The caption.</param>
        public void ShowInformation(string message, string caption)
        {
            // No implementation by design
        }

        /// <summary>
        /// Shows the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>The <see cref="MessageResult"/>.</returns>
        public MessageResult Show(string message)
        {
            // No implementation by design
            return MessageResult.OK;
        }

        /// <summary>
        /// Shows the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="caption">The caption.</param>
        /// <returns>The <see cref="MessageResult"/>.</returns>
        public MessageResult Show(string message, string caption)
        {
            // No implementation by design
            return MessageResult.OK;
        }

        /// <summary>
        /// Shows the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="button">The button.</param>
        /// <returns>The <see cref="MessageResult"/>.</returns>
        public MessageResult Show(string message, string caption, MessageButton button)
        {
            return Show(message, caption, button, MessageImage.None);
        }

        /// <summary>
        /// Shows the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="button">The button.</param>
        /// <param name="icon">The icon to show.</param>
        /// <returns>The <see cref="MessageResult"/>.</returns>
        public MessageResult Show(string message, string caption, MessageButton button, MessageImage icon)
        {
            if (ExpectedResults.Count == 0)
            {
                throw new Exception(Exceptions.NoExpectedResultsInQueueForUnitTest);
            }

            return ExpectedResults.Dequeue();
        }
        #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