Click here to Skip to main content
15,880,608 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 System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Catel.Test.IO
{
    [TestClass]
    public class DirectoryTest
    {
        #region DirectoryExists
        [TestMethod]
        public void DirectoryExists_ExistingDirectory()
        {
            // Declare variables
            string input = @"C:\Windows\";
            bool expectedOutput = true;

            // Call method
            bool output = Catel.IO.Directory.Exists(input);

            // Check result
            Assert.AreEqual(expectedOutput, output);
        }

        [TestMethod]
        public void DirectoryExists_NonExistingDirectory()
        {
            // Declare variables
            string input = @"C:\MyWindows\";
            bool expectedOutput = false;

            // Call method
            bool output = Catel.IO.Directory.Exists(input);

            // Check result
            Assert.AreEqual(expectedOutput, output);
        }

        [TestMethod]
        public void DirectoryExists_Null()
        {
            try
            {
                Catel.IO.Directory.Exists(null);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("directory", ex.ParamName);
            }
        }

        [TestMethod]
        public void DirectoryExists_EmptyParameter()
        {
            Assert.AreEqual(false, Catel.IO.Directory.Exists(string.Empty));
        }
        #endregion

        #region DeleteDirectory
        [TestMethod]
        public void DeleteDirectory()
        {
            // Get temporary directory
            string baseDirectory = Catel.IO.Path.Combine(System.IO.Path.GetTempPath(), "DeleteDirectoryTest");

            // Create test directories (5 levels deep)
            CreateDirectories(baseDirectory, 5);

            // Now remove the directories again
            bool output = Catel.IO.Directory.Delete(baseDirectory);

            // Validate output
            Assert.AreEqual(true, output);
            Assert.AreEqual(false, Catel.IO.Directory.Exists(baseDirectory));
        }
        #endregion

        #region Helper methods
        /// <summary>
        /// Creates a lot of directories with subdirectories and files.
        /// </summary>
        /// <param name="directory">The directory.</param>
        /// <param name="depth">The number of subdirectories to create.</param>
        public void CreateDirectories(string directory, int depth)
        {
            // Create directory
            System.IO.Directory.CreateDirectory(directory);

            // Create files in directory
            CreateFilesInDirectory(directory);

            // Should we still create directories?
            if (depth > 0)
            {
                // Create 10 subdirectories
                for (int i = 1; i < 5; i++)
                {
                    // Create subdirectory
                    CreateDirectories(Catel.IO.Path.Combine(directory, i.ToString()), depth - 1);
                }
            }
        }

        /// <summary>
        /// Creates 10 dummy files in the directory.
        /// </summary>
        /// <param name="directory">The directory.</param>
        private void CreateFilesInDirectory(string directory)
        {
            // Create files
            for (int i = 1; i <= 10; i++)
            {
                // Get filepath & create file
                string filePath = Catel.IO.Path.Combine(directory, i + ".dat");
                using (System.IO.FileStream fileStream = System.IO.File.Create(filePath))
                {
                    // If you want, write something to the file
                }
            }
        }
        #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