Click here to Skip to main content
12,696,822 members (29,129 online)
Click here to Skip to main content

Stats

25.4K views
523 downloads
11 bookmarked
Posted

Catel - Part 4 of n: Unit testing with Catel

, 28 Jan 2011 CPOL
This article explains how to write unit tests for MVVM using Catel.
src
Catel.Articles.04 - Unit testing
Models
Properties
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
ViewModels
Windows
Catel.Articles.04 - Unit testing.Test
Models
Properties
UI
ViewModels
Catel.Articles.Base
Data
Attributes
Properties
Resources
Images
CatenaLogic.png
Preview.png
Run.png
ShowCode.png
UI
Controls
Helpers
ViewModels
Windows
Catel.Core
Attributes
ClassDiagrams
DataObjectBase.cd
SavableDataObjectBase.cd
Collections
Helpers
ComponentModel
Data
Attributes
Exceptions
Interfaces
Diagnostics
Extensions
Helpers
Exceptions
Helpers
IO
Exceptions
IoC
LLBLGen
Log4net
Appender
Extensions
Helpers
MVVM
Commands
Interfaces
Exceptions
Services
EventArgs
Exceptions
Interfaces
ViewModels
Attributes
Interfaces
Properties
Reflection
Exceptions
Extensions
Helpers
Runtime
Serialization
Attributes
Helpers
Security
Cryptography
Helpers
Catel.Examples.Models
Properties
Catel.Examples.PersonApplication
Properties
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
ViewModels
Windows
Catel.Examples.Silverlight
Properties
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
Pages
ViewModels
Windows
Catel.Examples.Silverlight.Web
Catel.Examples.Silverlight.Web.csproj.user
ClientBin
Properties
Catel.FxCop
Catel.Silverlight
Diagnostics
log4net
Core
MVVM
Commands
Services
ViewModels
Properties
Catel.Core
Catel.Windows
Reflection
Themes
Generic
Assets
Old
Windows
Controls
Data
Converters
Helpers
Helpers
Catel.Silverlight.Test
Properties
Catel.Silverlight.Test.Web
Catel.Silverlight.Test.Web.csproj.user
ClientBin
Properties
Catel.snk
Catel.Templates.WpfApplication
Properties
UI
Controls
ViewModels
Windows
Catel.Templates.WpfItemTemplates
Properties
UI
Controls
ViewModels
Windows
Catel.Test
Collections
Convert
Data
Helpers
IO
MVVM
UI
ViewModels
Properties
Reflection
Runtime
Serialization
Security
Cryptography
Test References
Catel.Windows.accessor
Windows
Data
Converters
Catel.vsmdi
Catel.Windows
ClassDiagrams
ViewModelBase.cd
Collections
Extensions
Helpers
MVVM
Commands
Services
Test
UI
ViewModels
Properties
Resources
Images
Add.png
ClearOutput.png
Edit.png
Error.png
Loading.gif
Preview.png
Remove.png
Save.png
TipOfTheDay.png
Warning.png
Themes
Aero
ExpressionDark
Assets
Generic
Assets
Controls
Jetpack
Assets
background.png
Old
SunnyOrange
Assets
Windows
Controls
Extensions
LinkLabel
StackGrid
Data
Converters
Helpers
Documents
Extensions
Extensions
Helpers
Input
Markup
Media
Effects
EmptyEffect
EmptyEffect.fx
EmptyEffect.ps
EmptyEffect.fx
GrayscaleEffect
GrayscaleEffect.fx
GrayscaleEffect.ps
Extensions
Imaging
Extensions
Windows
DataWindow
TipOfTheDay
Local.testsettings
Settings.StyleCop
TraceAndTestImpact.testsettings
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)

Share

About the Author

Geert van Horrik
Software Developer CatenaLogic
Netherlands Netherlands
I am Geert van Horrik, and I have studied Computer Science in the Netherlands.

I love to write software using .NET (especially the combination of WPF and C#). I am also the lead developer of Catel, an open-source application development framework for WPF, Silverlight, WP7 and WinRT with the focus on MVVM.

I have my own company since January 1st 2007, called CatenaLogic. This company develops commercial and non-commercial software.

To download (or buy) applications I have written, visit my website: http://www.catenalogic.com

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 28 Jan 2011
Article Copyright 2011 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid