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.
using System.IO;
using Catel.Security.Cryptography;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Catel.Test.Security.Cryptography
{
    [TestClass]
    public class CryptHelperTest
    {
        #region Constants
        /// <summary>
        /// The file content that will used to calculate the hash.
        /// </summary>
        private const string Content = "abcdefghijklmnopqrstuvwxyz";

        /// <summary>
        /// The actual hash of the content.
        /// </summary>
        private const string ContentHash = "C3FCD3D76192E4007DFB496CCA67E13B";

        /// <summary>
        /// The hash of an empty string ("").
        /// </summary>
        private const string EmptyContentHash = "D41D8CD98F00B204E9800998ECF8427E";
        #endregion

        #region Variables
        private FilesHelper _filesHelper = new FilesHelper();
        #endregion

        #region Initialization and cleanup
        /// <summary>
        /// Cleans up.
        /// </summary>
        [TestCleanup]
        public void CleanUp()
        {
            if (_filesHelper != null)
            {
                _filesHelper.CleanUp();
                _filesHelper = null;
            }
        }
        #endregion

        #region CalculateMD5FileHash
        [TestMethod]
        public void CalculateMD5FileHash()
        {
            string fileName = _filesHelper.GetTempFileName();
            using (TextWriter textWriter = new StreamWriter(fileName))
            {
                textWriter.Write(Content);
            }

            string actualHash = CryptHelper.CalculateMD5FileHash(fileName);

            Assert.AreEqual(ContentHash, actualHash);
        }

        [TestMethod]
        public void CalculateMD5FileHashWithEmpyFile()
        {
            Assert.AreEqual(string.Empty, CryptHelper.CalculateMD5FileHash(""));
            Assert.AreEqual(string.Empty, CryptHelper.CalculateMD5FileHash(null));
        }
        #endregion

        #region CalculateMD5ContentHash
        [TestMethod]
        public void CalculateMD5ContentHash()
        {
            string actualHash = CryptHelper.CalculateMD5ContentHash(Content);

            Assert.AreEqual(ContentHash, actualHash);
        }

        [TestMethod]
        public void CalculateMD5ContentHashWithEmpyContent()
        {
            Assert.AreEqual(EmptyContentHash, CryptHelper.CalculateMD5ContentHash(""));
            Assert.AreEqual(string.Empty, CryptHelper.CalculateMD5ContentHash(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