Developer Testing Hints and Tips

Howdy happy campers.

I want to discuss a piece, somewhat divergent from the topic of physical coding, although still a facet of development that is close to my heart (and easy to overlook in many respects when constantly mashing keys and churning out code); developer testing. More specifically, I want to provide a set of guidelines that ‘may’ (insert disclaimer) help with the process and provide some food for thought.

This is in no way a definitive guide or best practice for that matter; more just a personal take on what I find works for me and the guts of a generally beneficial ‘templated’ approach to follow.

I would love to invite discussion on this one (or just get a take on what works for you), so please do hit me up on twitter or add a comment below, I’d love to hear from you.

My Process

As with any process, ground work and preparation can be vital for achieving a good result. To this end, I invariably start my developer testing on a given work item with a template document that looks like this:

Illustration of how to structure you Developer Testing.

Developer Testing Helper Document Structure.

What goes into your document will largely depend on what technologies you are using of course. For instance, you may never have a database centric element to the development you perform, rendering the ‘Database Upgrade’ section null and void ‘in all cases’. Ultimately, add and remove sections as you see fit but do strive for consistency. I myself test a mixture of items that may or may not include T-SQL elements. However, I choose to include the ‘Database Upgrade’ section in this case on every occasion, preferring to note that ‘there were no T-SQL’ related parts to the item, even just to mark it as ‘N/A’ (for my own sanity and for easy recollection later down the line, without the need to scan a lengthy list of changes elsewhere in the notes). Basically, my OCD kicks in and I start to wonder why I haven’t included a section that I ‘always’ include, leading to paranoia that I’ve missed something!

Each section (other than Notes), which is probably self-explanatory, can result in a PASS, QUERY or PASS-BACK state. Section state obviously knocks on and influences the result recorded against the ‘Developer Testing Summary’ header. PASS denotes an ‘A-Okay’ state, good to rock and roll! QUERY gives you the opportunity to mark a section with ‘discussion points’ or things you would like to check, without necessarily marking it off as incorrect (I tend to do this a lot, as I love to talk!). PASS-BACK is used in the circumstance whereby an error can be replicated/reproduced consistently or a logic problem definitely flies in the face of the ‘Acceptance Criteria’ for the story. In the circumstances whereby things such as coding standards have been contradicted I tend to use a mixture of QUERY/PASS-BACK, depending on the notes the developer has provided (it could be a flat PASS, of course, as there are always occasions where the rules need to be broken!).

So, section by section, let’s go over what we have and why…

Notes

It’s incredibly tempting to start diving into code, comparing files, trying to make sense of what the hell is going on but…I may get in trouble here, I’m going to tell you to stop right here. It’s so easy, and I’ve done it (probably) hundreds of times, to get eye deep in code, wasting large pots of time, before the basic question of ‘what are we doing and why’ has been answered. This is where this section comes in.

Use this area of your notes to compile a few short paragraphs (or bullet points, whatever you prefer) on the following:

  • Read over the developers notes and, after discovering if any changes have occurred to the underlying requirements for the story, start to create…
  • Your own summary of the ‘Acceptance Criteria’ for this particular story (or item, whatever term floats your boat. I’m going to use both interchangeably to alleviate bombarding you with the same term too much!).
  • Then, list any other pertinent information surrounding how the developer has coded the item (e.g. decisions that have shaped how the story has turned out). For example, did they place code into a different service than was originally expected because of ‘x’ reason, or did some logic end up in a different layer in the technology stack than conceived originally.
  • Lastly, note any of your initial thoughts, concerns or things you intend to check/look for based on this initial scoop of information.

The core reason I do this is to try to solidify my expectations, begin thinking about a test plan (yes, I like to always perform (rudimentary at the bare minimum) application testing, this isn’t just down to QA in my mind!) and to try to mitigate the chances of any massive surprises. Surprises, although they will always eventually happen one way or another, will lead to more confusion and increase the chances of things slipping through the net. You’ll be able to, by just following this exercise or a similar routine, cross-reference your expectations with the code changes you see and more easily be able to pick up errors, incorrect logic or unrequired alterations. This will limit the chances that something will slip past your mental filter as an ‘I guess that’s correct’ or ‘perhaps that class needed to be changed also, ok’ moment (don’t lie, we’ve all had them 😉 !).

Cool, we’ve formed in our own minds what this item is for, how it’s been developed and what, as a baseline, we are expecting to see. Let’s test (and along the way, discuss a few more tactics).

Database Upgrade

Some of what I’ll discuss here is formed around how my personal development role operates, so feel free to modify this approach to your needs. Again, if you don’t deal in the realm of database development at all pass go and collect £200, you’ve bypassed this step; congratulations!

The essence of this section surrounds you being able to state that new Stored Procedures, Functions, Views, Triggers, etc. can be ‘created’ without error on a database in a suitable ‘versioned’ state. Also, can ad-hoc data scripts, that are part of the development item, be run without error?

Some other considerations…

  • Are object creation scripts/ad-hoc scripts expected to be re-runnable? If yes, then specifically test and note this down here.
  • If you are in an environment whereby this kind of testing needs to be performed on multiple databases then mark this down here also (splitting notes down into sections against each target database/environment, whatever is applicable).
  • We work with a ‘versioned’ database so I make an effort to state which version I am on at the start of the testing run for reference.

An example of what this section may look like is illustrated below for reference:

Illustration of how to structure the Database Upgrade Developer Testing Document Section.

Developer Testing Database Upgrade Section Example.

A QUERY/PASS-BACK at this stage will bubble up and alter the status listed for the entire developer testing process. An additional note here; depending on how many queries/issues you find (and the length of the testing notes in general), you may want to copy the core query/error text to the top of the notes for easy review by the developer later (this applies to all of the following sections in fact).

Code Review

Moving on to the main filling of your developer testing sandwich, the actual code review! Obviously, you’ll be reviewing files here and looking at scripts, new files or amended code but definitely take a second or two out (unless your setup has automated builds/continuous integration, or some other clever solution, to tell you this) to make sure the code compiles before proceeding (and make the relevant note). A simple step but one easily forgotten, meaning you can get to the end of a code review before realising parts of the code don’t compile, eek!

I tend to, from a structural and sanity point of view (clarity is key), split my testing notes here into sections based on technology (i.e. T-SQL, C#, JavaScript, etc), or, at least, make some effort to order up a single list of files by file type. I tend to, for C# changes, group code files by the related project (given that projects should represent a logical grouping of types, hence allowing you to dice up changes by functional area, i.e. common extensions, data access helpers, etc.).

The point that you should take away from this, however, is that a little bit of thought and structuring at this phase will make your life easier; especially as a number of code files rack up.

If you’re looking for a small sample on how this section could look, after being fleshed out, then here you go:

Illustration of how to structure the Code Review Developer Testing Document Section.

Developer Testing Code Review Section Example.

However, what about the code review procedure itself I hear you cry! What follows next shouldn’t be taken as an exhaustive list, or correct in every given situation for that matter; more just suggestions as to what I’ve found helpful over time (mental kit bag):

  • For C# (and other object-orientated languages that support this concept), ensure that null values are correctly handled. Whether this is by capturing nulls on call to a given method and throwing an ArgumentNullException, or by doing a ‘not equal to null’ check (!= null) around code that would otherwise fail.
  • Strings can be tricky buggers, especially in case-sensitive environments! In most cases, comparisons should be performed taking case-sensitivity out of the equation (another case-by-case situation of course). I’d keep an eye out, again for C#, for the correct use of String.ToUpperInvariant, String.ToLowerInvariant and String.Equals. For String.Equals, use an overload containing a StringComparison enumeration type, for case/culture-insensitive options.
  • Keep an eye out for instances of checks being performed against strings being null or an empty string (either one or the other only). This can quickly lead to chaos, switch out for a null, empty or whitespace check (e.g. String.IsNullOrWhiteSpace).
  • Empty try/catch handlers are evil. Kill any you find.
  • Check up for instances whereby a class consists of all static members, but the class is not marked as static.
  • Train the eye to look for casting operations; you’ll always catch a few where the casting operation ‘could’ throw exceptions and should, therefore, be subject to more careful handling.
  • Big bugbear in the realm of coding; if a method requires scrolling to get through it’s a significant indication right off the bat that it is a prime candidate for refactoring. Unless there is a good reason, or it is clearly performing one logical function, consider having a conversation about breaking the method down.
  • Look for missed opportunities to rational code using inheritance. The most common one I see (and forget myself) is the abstraction of code to base classes and then using virtual methods/overrides in subclasses. Hawk-eye for types that should be abstract.
  • A simple one, but something that could easily slap you in the face if you’re not careful. When ‘language switching’, in a DT sense, take a second to make a mental note that you should be changing mind-sets (i.e. syntax is changing, get your game-face on!). For example, you stare into the abyss of C# for long enough (seeing ‘!= null’) you may, on switching to T-SQL, not notice a ‘!= NULL’ that should have been an ‘IS NOT NULL’. Those trees can be damn hard to find in the woods, after all!
  • Watch out for expensive operations, whereby values should be obtained once, ideally, then cached. It can be easy to let code skip by that repeatedly calls a database, for instance, to the detriment of performance (or possible errors, depending on the nature of the functionality called).
  • I love, love, loooovvvveeeee comments! Probably (ok, to the levels of being a little OCD about it!) too much. As far as C# code goes, I prefer (but don’t fail on this basis alone) XML Comments for C# and like to see comments on bulkier pieces of T-SQL. If there is a sizeable piece of code, whereby its function stretches beyond ‘trivial’, I like to see at least a short statement stating intent (what the developer is expecting the code to do is key by the way…as discussed next).
  • Where you have comments, link the intent in these comments back to what the code is actually doing; then trail it back to the items ‘Acceptance Criteria’ where appropriate. I have been rescued (as a developer, submitting my work for DT) countless times by those performing DT on my code, just by someone relaying to me that ‘what I thought my code was doing’ (based on my comments) doesn’t tie up to the actual functionality being offered up. This has led to me, of course, face-palming myself but being relieved that the gap in my intent, when checked off against my actual code, had been picked up by somebody in time to catch it before QA (or deployment, gulp!). State intent, then reap the rewards when mistakes you make are more rapidly picked up for rectification.
  • Be sure to look for the use of language constructs/keywords or syntactic-sugar that is not permissible on your baseline, minimum supported environment (i.e. older versions of SQL Server or .NET), if what you work on has this concept of course. This is sure to be something that will get picked up by QA causing bounce backs, or by your consumers later on if you’re not careful!
  • Keep a look out for code that could (or should) be shared or has been placed in a project/location that does not make logical sense. At a bare minimum, picking up on this sooner rather than later will keep your code base tidier, allow for ample opportunities to put great code in places to be leveraged as much as possible. In other cases, asking these kinds of questions can expose flaws and issues with the way a solution has been architected, which occasionally will steer you clear of tight spots later down the line.
  • Where shared code has been changed, look for instances whereby other applications/areas of the code base could be broken as a result of the changes. Recompile code to check for this as required. I had a bite on the bum by this recently :-?.
  • Keep up to date with any coding standards documents that should be adhered to and make sure the guidelines are followed (within reason of course; you’ll always find a scenario whereby a rule can, and should, be broken).
  • Really do consider writing and using Unit Tests wherever possible. They are a useful facet in the grand scheme of things (I believe at least) and they do carry weight when pitched up against visually checking code and application testing in general.
  • Last little nuggets, which I see from time to time. Look for objects constantly being created inside loops, heavy amounts of string concatenation not using the correct constructs (e.g. a StringBuilder in C#) or missed opportunities to create sub Stored Procedures in T-SQL (sectioning off code to gain performance boosts and obtain better execution plans). In fact, for T-SQL it can be a useful exercise to check the performance of non-trivial pieces of code yourself by changing how it’s structured, whilst obtaining the same results of course. You may or may not be able to increase performance along the way, but you’ll have far better comprehension of the code by the end regardless.

Hopefully, this little snapshot from my bag o’ tricks is enough to get you started, or get the brain-juices flowing. Let me know what you think of these suggestions anyway; I’d really appreciate the opportunity to collate others general thoughts and get a collective consensus going.

Application Testing

Here is where I will defer the giving of advice to my beloved QA counterparts on this beautiful planet; this, of course, isn’t my area of expertise. My only opinion here is (developers will possibly hate me for stating it) that developers ‘should’ always perform application testing alongside a code review. In fact, I’m a keen advocate for developers being involved in performing QA on the odd occasion. I personally like doing this, provided I have a trusty QA on hand to assist me (thankfully, I work with the best one around ;-), so no worries there). The simple reasons for this are:

  • One way or the other, acquisition of Product Knowledge is going to be invaluable to you. It’s just as valuable to start using your products in anger as it is to analyse code for hours on end. The side-note here is that this is part of your overall ‘worth’ as a developer, so don’t neglect it.
  • At this stage, you get to think as the customer might. Ideas and thoughts you have at this stage, which direct more development or changes to the product, will be amongst some of the best (and most rewarding when it comes to getting that warm and fuzzy feeling!).
  • Urm…it’s embarrassing to say ‘oh yeah, that codes great, thumbs up!’ for it then to explode in someone else’s face on the first press of a button! Easily avoided by following the process through from end to end, no matter what.

Ok, I’ll have a go at channelling one QA thought. Ok, I got it, here’s one from a mysterious and wise QA guru:

Mysterious and wise guru here… a friendly reminder to developers…never, ever, test your items using only one record! The reason? Well, I’ll test it with more than one record and break it instantly!

If anyone doing QA reads this feel free to feed us your arcane knowledge…God knows we need it! I would advise you keep the original item requirements in mind here of course, whilst testing; securing any process variants in your thoughts that could potentially throw carefully laid plans to waste (e.g. what if we go back and forth from screens x and y between completing process z, or we save the same form information twice, etc.). Your knowledge of the code can help at this stage so use the opportunity whilst you have it.

Before I forgot, an example of this could look like this:

Illustration of how to structure the Application Testing Developer Testing Document Section.

Developer Testing Application Testing Section Example.

Code Review/Application Testing – The Most Important Point…

Do it!!! If you’re not sure (as I am still on a regular basis) then ask the question and run the risk of looking like an idiot! Be a spanner, who cares at the end of the day. I dread to think of how many developers have stared at code and, ultimately, let stuff slide because they refused to pipe up and just say they weren’t sure or ‘didn’t get it’. At the end of the day, it’s better to ask questions and if there turns out to be no issues, or it’s a simple misunderstanding, then no harm, no foul. On a good number of occasions I query things to later realise that I missed a line of code meaning it does work as intended, or there’s some process that had slipped my mind…it hasn’t got me sacked (ahem, yet!). So my advice is just to open up and have a natter at the end of day, it’ll be worth the ratio of ‘idiot’ to ‘bug-saving’ moments, trust me :-).

Admin

As with any process, there will always be (and if there isn’t for you then let me know where you work because it’s awesome!) a certain amount of ‘red tape’. Use this last section to keep track of whether any procedural bits and bobs have been handled. For example, I’m expected to cover the creation of a Release Note (as part of the practices I follow) for any item I work on, so it should be marked down in this section as to whether I’ve completed it or not. It could end up just being a very simple section, like the following:

Illustration of how to structure the Admin Developer Testing Document Section.

Developer Testing Admin Section Example.

I hope this has been helpful and informative; or, at least, got the mind going to start thinking about this process. Again, as mentioned above, I would love to hear your thoughts so please do get in touch either here or via social media.

Cheers all, keep smacking keys and producing coding loveliness in the meantime 🙂

When are Injections Fun? C# Dependency Injection Deep Dive

Evening all,

This post has been in the works for a while but, due to a wee bit of sickness and a sudden busy schedule outside of work, it’s been waiting in the wings for a little longer than it should have been. Apologies about that guys and girls!

Anyway, I wanted to expose a piece on Dependency Injection. There has been an ongoing slice of development, using this methodology, doing the rounds at work. However, I wanted to look at DI from the perspective of Containers (DI can exist without these, but I’m interested solely in this approach for the time being) and will be using Autofac as the showcase for this.

Dependency Injection In A Nutshell

Dependency Injection essentially encapsulates the idea of producing loosely-coupled, easily testable code whereby classes are not (in the case of coupling DI with interface usage) tied to each other using concrete types. Using a DI Container, it’s also possible for the caller to request an implementation via an interface and retrieve an object on demand, without the need to worry about what other dependencies the type requested may require. When things are interface-centric, it’s also a cinch to override implementations and, off the back of this, unit test specific type behaviour in a more isolated way.

The best explanation you’ll find, without a shadow of a doubt, is this one (enjoy!):

How to explain dependency injection to a 5-year-old?

When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn’t want you to have. You might even be looking for something we don’t even have or which has expired.

What you should be doing is stating a need, “I need something to drink with lunch,” and then we will make sure you have something when you sit down to eat.

Dependency Injection Sample

What follows is a fairly trivial application that implements an Autofac DI Container, and shows off a few basic DI concepts to boot. Firstly, here are the resources that I used to kick start this project:

Autofac Documentation
Autofac NuGet Gallery Reference

I found this particular TechEd video particularly interesting. It covers DI and Container usage from the perspective of Autofac, Unity and many others (although, leans more towards Autofac as the examples begin to get more fleshed out):

Deep Dive into Dependency Injection and Writing Decoupled Quality Code and Testable Software

This first snippet of code outlines the entry point for a small Console Application. The ConfigureApplicationContext method constructs a ContainerBuilder object, which is part of the Autofac namespace, using a RegisterApplicationTypes helper method. This method illustrates, on application start, the linking of interface types to concrete implementations. Notice the addition of the ‘SingleInstance’ call against the registration of the IConfigurationHelper implementing type, this is an interesting nugget that forces the same instance of a class, as opposed to a new instance, to be retrieved when required (via a request to the DI Container).

The FileCleaner classes Run method is then called (I’ve used a concrete type at this level but, of course, I could have referenced this via an interface also). The FileCleaner class constructor accepts, for the purposes of resolving types from the DI Container, an IProcessorLocator implementing object. You’ll see this isn’t passed in per say, the call to Application.Container.Resolve takes care of this ‘dependency’ for use, magic! You’ll see how the IProcessorLocator object gets used specifically in later examples (the comments here also expand on the concepts at work, so be sure to have a sift through).

using Autofac;
using DesktopCleaner.Helpers;
using DesktopCleaner.Implementations;
using DesktopCleaner.Interfaces;
using DesktopCleaner.Locators;
using System;

namespace DesktopCleaner
{
    /// <summary>
    /// Console Application Program Class, containing this applications
    /// entry point (Main) method.
    /// </summary>
    class Program
    {
        #region Constructor

        /// <summary>
        /// Static Main method representing this applications
        /// entry point.
        /// </summary>
        /// <param name="args">Any arguments from external sources.</param>
        static void Main(string[] args)
        {
            // Configure this applications DI Container
            ConfigureApplicationContext();
        }

        #endregion Constructor

        #region Private Static Helper Methods

        /// <summary>
        /// Private static helper method that configures this applications
        /// DI Container fully, ready for the applications life-time (as well
        /// as kicking off the core processing of this application via a FileCleaner instance).
        /// </summary>
        private static void ConfigureApplicationContext()
        {
            // Use the Autofac ContainerBuilder type to construct interface/concrete type mappings
            ContainerBuilder builder = RegisterApplicationTypes();

            // Construct our DI Container (for general use throughout the lifetime of our application in this case)
            Application.Container = builder.Build();

            // Create a cleanerHelper and 'run it' (the objects constructor will receive an IProcessLocator implementing object (ProcessorLocator) in this case due to the previous mappings)
            FileCleaner cleanOperationHelper = Application.Container.Resolve<FileCleaner>();
            if (cleanOperationHelper.Run())
            {
                Console.WriteLine("Cleaning Completed Successfully!");
            }
            else
            {
                Console.WriteLine("Cleaning Failed. Please check the logs (what a joke, there are none!).");
            }

            // Halt application before exit so we can inspect the output
            Console.ReadLine();
        }

        /// <summary>
        /// Private static helper method that creates a ContainerBuilder
        /// to map interfaces to concrete types (for DI loveliness). Return the builder
        /// to the caller (we're using this to called builder.Build() to setup our 'Container').
        /// </summary>
        /// <returns></returns>
        private static ContainerBuilder RegisterApplicationTypes()
        {
            // Instantiate a builder and map types as required
            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<FileCleaner>();
            builder.RegisterType<ConfigurationHelper>().As<IConfigurationHelper>().SingleInstance();    // A special case here, demonstrate the use of SingleInstance()
            builder.RegisterType<FileHelper>().As<IFileHelper>();
            builder.RegisterType<ProcessorLocator>().As<IProcessorLocator>();                           // A cool little utility to get instances (so we don't have to new-up in classes using concrete types)

            return builder;
        }

        #endregion Private Static Helper Methods
    }
}

Absolutely not required, just for illustration purposes only, the following static class is simply used as an application level helper (for global access to the container). Not necessarily how you’d structure it, but makes the examples coming up easier to manoeuvre through.

using Autofac;

namespace DesktopCleaner.Helpers
{
    /// <summary>
    /// Public static class that holds application constants
    /// and shared helpers.
    /// </summary>
    public static class Application
    {
        #region Public Static Properties (CORE DI CONTAINER)

        /// <summary>
        /// Public static property (for use across this application
        /// </summary>
        public static IContainer Container { get; set; }

        #endregion Public Static Properties (CORE DI CONTAINER)

        #region Public Constants

        /// <summary>
        /// Public constant string representing the DesktopAppSettingKey
        /// key value (in the app.config).
        /// </summary>
        public const string DesktopAppSettingKey = "DesktopPath";

        #endregion Public Constants
    }
}

Next up, we need some interfaces. These represent key processes that our application is expected to implement (the catch being that these interfaces will be used to retrieve concrete implementations on demand, without the need to use the ‘new’ keyword in our classes). Note the last interface in this list, IProcessorLocator. Again, this is a linchpin type that will be passed to each class to retrieve all manner of other types (due to its use of a generic method) on demand. The result being here that using an IProcessorLocator, as a container wrapper, negates the need to pass multiple interfaces to a type’s constructor. We just pass this one interface supporting object instead (and this also means we don’t need data fields to store these instances either).

namespace DesktopCleaner.Interfaces
{
    /// <summary>
    /// Public ICleanerHelper support interface definition.
    /// </summary>
    public interface ICleanerHelper
    {
        #region Interface Methods

        /// <summary>
        /// Public method for ICleanerHelper support representing
        /// behaviour for a 'run' process method.
        /// </summary>
        /// <returns>True if the run processing is successful, otherwise false.</returns>
        bool Run();

        #endregion Interface Methods
    }
}

...

using System.Collections.Specialized;

namespace DesktopCleaner.Interfaces
{
    /// <summary>
    /// Public ICleanerHelper support interface definition.
    /// </summary>
    public interface IConfigurationHelper
    {
        #region Interface Methods

        /// <summary>
        /// Public method for IConfigurationHelper support to allow for behaviour
        /// to retrieve a 'setting' based on a 'key'.
        /// </summary>
        /// <param name="key">The key to transpose to a value.</param>
        /// <returns>The value that links to the specified key.</returns>
        string GetConfigurationSetting(string key);

        /// <summary>
        /// Public method for IConfigurationHelper support to allow for behaviour
        /// to retrieve a collection of 'setting' keys and values.
        /// </summary>
        /// <returns>A collection of settings (specialised NameValueCollection).</returns>
        NameValueCollection GetAllConfigurationSettings();

        #endregion Interface Methods
    }
}

...

using System.Collections.Generic;

namespace DesktopCleaner.Interfaces
{
    /// <summary>
    /// Public IFileHelper support interface definition.
    /// </summary>
    public interface IFileHelper
    {
        #region Interface Methods

        /// <summary>
        /// Public method for IFileHelper support to allow for behaviour
        /// to get a list of files based on the specified path (in some manner, based on implementation).
        /// </summary>
        /// <param name="path">The directory path to be used.</param>
        /// <returns>A list of files relating to the directory specified.</returns>
        List<string> GetFilesForPathAsList(string path);

        /// <summary>
        /// Public method for IFileHelper support to allow for behaviour
        /// to process a list of file names (in some manner, based on implementation). 
        /// </summary>
        /// <param name="fileList">Represents the list of files to operate on.</param>
        /// <returns>A boolean to represent if processing was successful.</returns>
        bool ProcessFiles(List<string> fileList);

        #endregion Interface Methods
    }
}

...

namespace DesktopCleaner.Interfaces
{
    /// <summary>
    /// Public IProcessorLocator support interface definition.
    /// </summary>
    public interface IProcessorLocator
    {
        #region Interface Methods

        /// <summary>
        /// Public method for IProcessorLocator support to allow for behaviour
        /// to get a 'processor' for any specified interface type (this should be constrained more
        /// in an ideal world).
        /// </summary>
        /// <typeparam name="T">The type to retrieve a processor class based on.</typeparam>
        /// <returns>A concrete class, depending on the implementation.</returns>
        T GetProcessor<T>();

        #endregion Interface Methods
    }
}

Here we have another ‘illustrative’ class that, as you’ll remember from our DI Container configuration, is marked as ‘Single Instance’ (just as an example). It simply interrogates the AppSettings of the app.config file to retrieve information, as required by calling code.

using DesktopCleaner.Interfaces;
using System.Collections.Specialized;
using System.Configuration;

namespace DesktopCleaner.Implementations
{
    /// <summary>
    /// Public ConfigurationHelper class that implements IConfigurationHelper
    /// to interrogate the applications app.config file (implementation can
    /// be modified however, as per the DesktopCleaner.UnitTests).
    /// </summary>
    public class ConfigurationHelper : IConfigurationHelper
    {
        #region Public Expression Bodied Methods

        // Again, illustration only, may abstract this into a differing format (use of lazy/properties, etc) - Methods are for demo purposes (for mocking up a functional class for DI usage)

        /// <summary>
        /// Gets access to the applications app.config AppSetting
        /// key/value pairs (as is).
        /// </summary>
        /// <returns></returns>
        public NameValueCollection GetAllConfigurationSettings() => ConfigurationManager.AppSettings;

        /// <summary>
        /// Gets access to the a particular AppSetting value, from the app.config
        /// file, based on the provided key.
        /// </summary>
        /// <param name="key">The AppSetting key to use when searching for a corresponding value.</param>
        /// <returns>A string denoting the matching value (based on key).</returns>
        public string GetConfigurationSetting(string key) => ConfigurationManager.AppSettings[key];

        #endregion Public Expression Bodied Methods
    }
}

The next couple of classes represent the bulk of our test application. There is nothing super special or uber magical here, so it should be easy to stick with. The idea behind this application is to move files from one location to another, tidying files after a successful move. This is all tied to configuration found in the app.config (which can, of course, be overridden as demonstrated later on by some example unit tests). Due to the use of interfaces, the implementation itself could differ greatly, however.

The key code lies in the constructor, that assigns a private data field with an object supporting the IProcessorLocator interface, allowing for type resolution in the other core methods in this class. Within the Run method the ‘processor locator’ is finally utilised to, on demand, locate and retrieve instances of IConfigurationHelper and IFileHelper supporting objects. We could, of course, have foregone the use of the ProcessLocator, and passed in all required interface types into this objects constructor. However, we would have incurred penalties for unrequired object creation and storage for types that, in reality, may not even be used by calling code (in this example I’m utilising all of the types anyway, but just mark it as a side note!). We have an example of some loosely-coupled code!

The CompareObjectsInDebug method is there just to illustrate that the call to processorLocator.GetProcessor is retrieving new objects/single instances as expected.

using DesktopCleaner.Helpers;
using DesktopCleaner.Interfaces;
using System;

namespace DesktopCleaner.Implementations
{
    /// <summary>
    /// Public class that represents a 'File Cleaner', supporting
    /// a Run method for processing and cleaning desktop files.
    /// </summary>
    public class FileCleaner : ICleanerHelper
    {
        #region Private Data Fields

        /// <summary>
        /// Private data field that represents this types processor locator (that
        /// will contain an instance of a class implementing this interface for 
        /// grabbing concrete types to perform operations.
        /// </summary>
        private IProcessorLocator processorLocator;

        #endregion Private Data Fields

        #region Constructor

        /// <summary>
        /// Instantiates a new FileCleaner object; Autofac deals with
        /// the passing of an IProcessorLocator supporting object (or Moq
        /// handles this when Unit Tests are involved).
        /// </summary>
        /// <param name="locator">An IProcessorLocator implementing object (for this class to support various forms of functionality).</param>
        public FileCleaner(IProcessorLocator locator)
        {
            processorLocator = locator;
        }

        #endregion Constructor

        #region Public Methods

        /// <summary>
        /// Public method that performs the core operation of this class. We obtain 
        /// information from the app.config, retrieve files from the designated desktop
        /// location and process them (i.e. copy to a set location and delete the originals).
        /// </summary>
        /// <returns>A boolean that denotes if this operation completed successfully.</returns>
        public bool Run()
        {
            Console.WriteLine("Running the cleaner helper!");

            // Use this instances IProcessLocator supporting object to get instances of classes supporting the IConfigurationHelper and IFileHelper interfaces
            // for retrieving files and subsequently processing them
            IConfigurationHelper configurationHelper = processorLocator.GetProcessor<IConfigurationHelper>();
            IFileHelper fileHelper = processorLocator.GetProcessor<IFileHelper>();

#if DEBUG
            // In debug, performing a little extra output logging to illustrate object setup (The IConfigurationHelper implementing object is set up as 'Single Instance', so we want to illustrate this)
            CompareObjectsInDebug(configurationHelper, fileHelper);
#endif

            // We have our objects, now run the 'process' and see if it completes successfully
            bool operationSuccessful = false;

            try
            {
                operationSuccessful = fileHelper.ProcessFiles(fileHelper.GetFilesForPathAsList(configurationHelper.GetConfigurationSetting(Application.DesktopAppSettingKey)));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return operationSuccessful;
        }

        #endregion Public Methods

        #region Private Helper Methods

        /// <summary>
        /// Private helper method that does a little bit of extra object
        /// interrogation and output, in debug mode, to show object configuration.
        /// </summary>
        /// <param name="configurationHelper">The IConfigurationHelper object passed from the caller (for comparison).</param>
        /// <param name="fileHelper">The IFileHelper object passed from the caller (for comparison).</param>
        private void CompareObjectsInDebug(IConfigurationHelper configurationHelper, IFileHelper fileHelper)
        {
            Console.WriteLine();

            // Create two new objects using the process locator - We want to see if the IConfigurationHelper supporting object is Single Instance, as opposed to the IFileHelper supporting object
            IConfigurationHelper configurationHelper2 = processorLocator.GetProcessor<IConfigurationHelper>();
            IFileHelper fileHelper2 = processorLocator.GetProcessor<IFileHelper>();

            // What are dealing with (additional debug info only) - Actual do the object interrogation; first on Hash Codes
            Console.WriteLine("configurationHelper HashCode: {0}", configurationHelper.GetHashCode());
            Console.WriteLine("configurationHelper2 HashCode: {0}", configurationHelper2.GetHashCode());
            Console.WriteLine("fileHelper HashCode: {0}", fileHelper.GetHashCode());
            Console.WriteLine("fileHelper HashCode: {0}", fileHelper2.GetHashCode());

            // ...Then on an actual 'equals' check
            Console.WriteLine("configurationHelper == ConfigurationHelper2: {0}", configurationHelper == configurationHelper2);
            Console.WriteLine("fileHelper == fileHelper2: {0}", fileHelper == fileHelper2);

            Console.WriteLine();
        }

        #endregion Private Helper Methods
    }
}

The next class, apart from being an example of very, very incomplete exception handling (gulp!) shows a similar pattern to the above class again. The IProcessorLocator object is passed in, as a dependency, to this object’s constructor (when it is resolved and retrieved by an object that requires it) and then utilised to retrieve types to perform the relevant class functions as needed.

This is essentially the workhorse class, handling files, performing the move and delete operations and then reporting the result (in the form of a boolean) to the caller. I’ve utilised a couple of private helper methods, just as processing aids (which could be changed as required/exposed also as public implementations to be overridden).

using DesktopCleaner.Helpers;
using DesktopCleaner.Interfaces;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace DesktopCleaner.Implementations
{
    /// <summary>
    /// Public class that represents a 'File Helper', supporting
    /// various methods for obtaining files and cleaning directories.
    /// </summary>
    public class FileHelper : IFileHelper
    {
        #region Private Data Fields

        /// <summary>
        /// Private data field that represents this types processor locator (that
        /// will contain an instance of a class implementing this interface for 
        /// grabbing concrete types to perform operations.
        /// </summary>
        private IProcessorLocator processorLocator;

        #endregion Private Data Fields

        #region Constructor

        /// <summary>
        /// Instantiates a new FileHelper object; Autofac deals with
        /// the passing of an IProcessorLocator supporting object (or Moq
        /// handles this when Unit Tests are involved).
        /// </summary>
        /// <param name="locator">An IProcessorLocator implementing object (for this class to support various forms of functionality).</param>
        public FileHelper(IProcessorLocator locator)
        {
            processorLocator = locator;
        }

        #endregion Constructor

        #region Public Methods

        /// <summary>
        /// Public method that, based on a provided path, will return
        /// files in the given directory in the form of a List<string>.
        /// </summary>
        /// <param name="path">The path to return files for.</param>
        /// <returns>A list of type string representing files in the given directory.</returns>
        public List<string> GetFilesForPathAsList(string path)
        {
            // Two phases of validation here. Ensure the path provided is set and is valid (treating these as both exceptional circumstances in my scenario, could be debated of course)
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (!Directory.Exists(path))
            {
                throw new InvalidOperationException("GetFilesForPathAsList called with a path that does not resolve to a valid directory");
            }

            // Attempt to set the directoryFileList so the data can be returned to the caller
            List<string> directoryFileList = null;

            try
            {
                directoryFileList = Directory.GetFiles(path).ToList();
            }
            catch (Exception ex)
            {
                // TODO (LOL!) - Log
                Console.WriteLine(ex.Message);
            }
            
            return directoryFileList;
        }

        /// <summary>
        /// Public method that serves as the main work-horse for this class.
        /// We consume a file list and, based on app.config AppSetting pathing information,
        /// decided on where to copy files based on extensions (subsequently deleting them from
        /// the original file location after copy).
        /// </summary>
        /// <param name="fileList">The list of files to operate on.</param>
        /// <returns>A boolean representing if the process was a success (fails on first error in its current form).</returns>
        public bool ProcessFiles(List<string> fileList)
        {
            // Phase one validation only, just ensure the file list is set correct (i.e. not null)
            if (fileList == null)
            {
                throw new ArgumentNullException(nameof(fileList));
            }

            bool filesProcessedSuccessfully = false;

            // Not the greatest here! For illustration only, get a class instance to interrogate app.config AppSettings and use this information to clean the file list data
            try
            {
                IConfigurationHelper configurationHelper = processorLocator.GetProcessor<IConfigurationHelper>();

                List<KeyValuePair<string, string>> appSettings = GetAppSettingsForFileProcessing(configurationHelper);
                CopyAndDeleteFilesBasedOnAppSettings(fileList, appSettings);

                // If we get here then we're successful (by definition that the code didn't error only, of course!)
                filesProcessedSuccessfully = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return filesProcessedSuccessfully;
        }

        #endregion Public Methods

        #region Private Static Helper Methods

        /// <summary>
        /// Private static helper method that uses the provided IConfigurationHelper to 
        /// retrieve AppSettings as a KeyValuePair list (essentially just for convenience).      
        /// /// </summary>
        /// <param name="configurationHelper">The IConfigurationHelper supporting class to retrieve settings based on.</param>
        /// <returns>A list of type KeyValuePair containing the various settings.</returns>
        private static List<KeyValuePair<string, string>> GetAppSettingsForFileProcessing(IConfigurationHelper configurationHelper)
        {
            // Prepare a KeyValuePair list for storing the settings in a more convenient format
            List<KeyValuePair<string, string>> appSettings = new List<KeyValuePair<string, string>>();

            // Using a little linq, get every AppSetting key and value and add it to the appSettings list (ignoring the DesktopAppSettingKey, we don't need this for file processing, so pretty much hard coded for purpose :o) )
            configurationHelper.GetAllConfigurationSettings().AllKeys.Where(appSettingKey => !appSettingKey.Equals(Application.DesktopAppSettingKey, StringComparison.InvariantCultureIgnoreCase)).ToList().ForEach(appSettingKey =>
            {
                appSettings.Add(new KeyValuePair<string, string>(appSettingKey, configurationHelper.GetConfigurationSetting(appSettingKey)));
            });

            // Return the results to the caller
            return appSettings;
        }

        /// <summary>
        /// Private static helper method that consumes a file list and 'settings' to perform
        /// a copy and delete process on each file.
        /// </summary>
        /// <param name="fileList">The files to operate on.</param>
        /// <param name="appSettings">The settings to apply to the files regarding copy and delete processing.</param>
        private static void CopyAndDeleteFilesBasedOnAppSettings(List<string> fileList, List<KeyValuePair<string, string>> appSettings)
        {
            // Loop through each file to process individually (just for illustration)
            string copyLocation = null, fileNameOnly = null;
            fileList.ForEach(file =>
            {
                // Retrieve the file name (only) associated with the path, and log to the console
                fileNameOnly = Path.GetFileName(file);
                Console.WriteLine("Attempting to clean: {0}", fileNameOnly);

                // Retrieve the copy location for this particular file extension, if one is set (again, probably better ways to do this, illustration only)
                copyLocation = appSettings.FirstOrDefault(item => item.Key.Equals(Path.GetExtension(file).Remove(0, 1), StringComparison.InvariantCultureIgnoreCase)).Value;

                // If we have a location to copy to then attempt to perform the copy (then delete the file afterwards from its original location). Failure will halt the process
                if (!string.IsNullOrWhiteSpace(copyLocation))
                {
                    File.Copy(file, Path.Combine(copyLocation, fileNameOnly));
                    File.Delete(file);
                }
            });
        }

        #endregion Private Static Helper Methods
    }
}

Lastly, here is the (very, very simple as it turns out!) wrapper for our DI Container, the ProcessorLocator (implementing IProcessorLocator).

using Autofac;
using DesktopCleaner.Helpers;
using DesktopCleaner.Interfaces;

namespace DesktopCleaner.Locators
{
    /// <summary>
    /// Public class that represents a 'Processor Locator', which
    /// just obtains concrete class based on the DI Container in the Program
    /// class (held statically in the DesktopCleaner.Helpers.Application class, which
    /// is set up when this application is started).
    /// </summary>
    public class ProcessorLocator : IProcessorLocator
    {
        #region Public Expression Bodied Methods

        /// <summary>
        /// Based on T provide a concrete class implementation
        /// (that matches the interface in the DI Container).
        /// </summary>
        /// <typeparam name="T">The interface to get a concrete type based on.</typeparam>
        /// <returns>An instance of the concrete class, as mapped.</returns>
        public T GetProcessor<T>() => Application.Container.Resolve<T>();

        #endregion Public Expression Bodied Methods
    }
}

Just for kicks! For anyone wondering what is found in the app.config, here it is for reference:

<!--App Setting Configuration example (within the app.config)-->
<appSettings>
  <add key="DesktopPath" value="C:\Users\fakeuser\Desktop\Imaginary_Desktop"/>
  <add key="jpg" value="C:\Users\fakeuser\Desktop\Imaginary_Desktop\jpeg_folder"/>
  <add key="url" value="C:\Users\fakeuser\Desktop\Imaginary_Desktop\url_folder"/>
  <add key="txt" value="C:\Users\fakeuser\Desktop\Imaginary_Desktop\txt_folder"/>
  <add key="xlsx" value="C:\Users\fakeuser\Desktop\Imaginary_Desktop\xslx_folder"/>      
</appSettings>

Another reference piece just to finish up before we inspect the actual output; a class diagram for the application as it stands (pretty flat and easy to understand):

Desktop Cleaner class diagram showing application structure.

Desktop Cleaner Class Diagram.

So, what do you get when you run this application? The next screenshot should hopefully give a good indication as to what is going on ‘under the hood’.

Here’s the final output, with the most interesting portion of output being in the first section, showing some details about the objects retrieved from the DI Container (using the ProcessorLocator class). This shows that IFileHelper instances (FileHelper in this case) are retrieved as new instances, whereby the IConfigurationHelper instances are single instances as expected (as per the DI Container configuration):

Console output from the Desktop Cleaner application.

Desktop Cleaner Report.

We now have a rough sample of loosely coupled code without concrete implementations embedded. Let’s have a look at the implications for unit testing.

DI and Unit Testing (Moq)

Does this methodology actually help during a basic unit testing scenario? I’ll be using Moq to find out:

Autofac.Extras.Moq NuGet Gallery Reference

In this example, we (after doing a little bit of setting up) utilise the AutoMock.GetLoose (get automatic mocks using loose mocking behaviour) method to retrieve a type to generate our ‘systems under test’ (i.e. other classes). The coolest thing here is that, as demonstrated by the FileHelperProcessFilesTest method, is the injection of the testMoqConfigurationHelper (which is retrieved when the type under test utilises an IProcessLocator to retrieve an IConfigurationHelper supporting object). This way, we are able to ‘rig’ certain parts of an implementation to focus down our unit tests to only specific areas of business logic, certainly very neat indeed. I’ve included all of the implementation code for reference (all commented to add clarity where required).

More details can be found here for those who want to delve a little deeper in Moq:

Moq Documentation

using Autofac.Extras.Moq;
using DesktopCleaner.Implementations;
using DesktopCleaner.Interfaces;
using DesktopCleaner.UnitTests.Helpers;
using DesktopCleaner.UnitTests.MoqObject;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace DesktopCleaner.UnitTests
{
    /// <summary>
    /// Public class that holds all of the Unit Test logic to
    /// run Moq tests against the DesktopCleaner application.
    /// </summary>
    [TestClass]
    public class DesktopCleanerTests
    {
        #region Private Static Data Fields (For Unit Tests)

        /// <summary>
        /// Private data fields that represent paths for directories
        /// to used during these unit tests (purely for demonstration).
        /// </summary>
        private static string testDesktopPath = Path.Combine(SharedTestValues.DebugPath, SharedTestValues.TestDesktopFolder),
            testTxtFilesPath = Path.Combine(testDesktopPath, SharedTestValues.TestTxtFilesFolder),
            testSqlFilesPath = Path.Combine(testDesktopPath, SharedTestValues.TestSqlFilesFolder);

        #endregion Private Static Data Fields (For Unit Tests)

        #region Test Class Initialisation Logic

        /// <summary>
        /// Public method that sets up the required resources for all
        /// of the Unit Tests featured in this class. NOTE: There is a requirement 
        /// for all of the tests to run (you wouldn't want to introduce a 'link' between all
        /// of your tests in production, but as I'm doing a demo this is permissible for now).
        /// </summary>
        /// <param name="context">The TestContext object for this class.</param>
        [ClassInitialize]
        public static void ConfigureTestResources(TestContext context)
        {
            // Create a Test Desktop folder and a TXT/SQL folder as sub-directories (ready to receive content in the tests below)
            Directory.CreateDirectory(testDesktopPath);
            Directory.CreateDirectory(testTxtFilesPath);
            Directory.CreateDirectory(testSqlFilesPath);

            // Add four tests files, two SQL files and two standard TXT file, into the root directory (for 'cleaning' during the unit tests)
            File.Create(Path.Combine(testDesktopPath, SharedTestValues.TestTxtFileNameOne)).Close();
            File.Create(Path.Combine(testDesktopPath, SharedTestValues.TestTxtFileNameTwo)).Close();
            File.Create(Path.Combine(testDesktopPath, SharedTestValues.TestSqlFileNameOne)).Close();
            File.Create(Path.Combine(testDesktopPath, SharedTestValues.TestSqlFileNameTwo)).Close();
        }

        #endregion Test Class Initialisation Logic

        #region Public Static Test Methods

        /// <summary>
        /// Public test method that runs an implementation test against
        /// a FileHelper class and the GetFilesForPathAsList method.
        /// </summary>
        [TestMethod]
        public void FileHelperGetFilesForPathAsListTest()
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                // Arrange (i.e. configure the mock)
                FileHelper sut = mock.Create<FileHelper>();

                // Act
                List<string> directoryList = sut.GetFilesForPathAsList(new MoqConfigurationHelper().GetConfigurationSetting(SharedTestValues.DesktopTestAppSettingKey));

                // Assert
                Assert.AreEqual(4, directoryList.Count);
            }
        }

        /// <summary>
        /// Public test method that runs an implementation tests against
        /// a FileHelper class and the ProcessFiles method primarily (ensuring that 
        /// the FileHelper receives a 'mock' version of an IConfigurationHelper for 
        /// testing purposes, which returns a predefined configuration to test against).
        /// </summary>
        [TestMethod]
        public void FileHelperProcessFilesTest()
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                // Arrange (i.e. configure the mock)
                MoqConfigurationHelper testMoqConfigurationHelper = new MoqConfigurationHelper();

                // This is the interesting bit - Pass the MoqConfigurationHelper as our implementation of IConfigurationHelper. This turns up in the constructor of our FileHelper (for use in the Unit Test)
                mock.Mock<IProcessorLocator>().Setup(loc => loc.GetProcessor<IConfigurationHelper>()).Returns(testMoqConfigurationHelper);
                FileHelper sut = mock.Create<FileHelper>();

                // Act
                List<string> directoryList = sut.GetFilesForPathAsList(testMoqConfigurationHelper.GetConfigurationSetting(SharedTestValues.DesktopTestAppSettingKey));

                // Assert
                Assert.IsTrue(sut.ProcessFiles(directoryList));
                Assert.IsFalse(Directory.GetFiles(testTxtFilesPath).Any(file => !Path.GetExtension(file).Equals(".txt")));
                Assert.IsFalse(Directory.GetFiles(testSqlFilesPath).Any(file => !Path.GetExtension(file).Equals(".sql")));
            }
        }

        #endregion Public Static Test Methods

        #region Test Class Cleanup Logic

        /// <summary>
        ///  
        /// </summary>
        [ClassCleanup]
        public static void CleanupTestResources()
        {
            // Remove traces of test files, for our next run (as this is all plonked in the debug/bin folder, not what you'd want for a genuine test setup most likely)
            Directory.GetFiles(testTxtFilesPath).ToList().ForEach
                (
                    file => 
                    {
                        File.Delete(file);
                    }
                );

            Directory.GetFiles(testSqlFilesPath).ToList().ForEach
                (
                    file => 
                    {
                        File.Delete(file);
                    }
                );

            Directory.GetFiles(testDesktopPath).ToList().ForEach
                (
                    file => 
                    {
                        File.Delete(file);
                    }
                );

            // Remove any test directories created for any run Unit Tests
            Directory.Delete(Path.Combine(SharedTestValues.DebugPath, SharedTestValues.TestDesktopFolder, SharedTestValues.TestTxtFilesFolder));
            Directory.Delete(Path.Combine(SharedTestValues.DebugPath, SharedTestValues.TestDesktopFolder, SharedTestValues.TestSqlFilesFolder));
            Directory.Delete(Path.Combine(SharedTestValues.DebugPath, SharedTestValues.TestDesktopFolder));
        }

        #endregion Test Class Cleanup Logic
    }
}
using System;
using System.IO;

namespace DesktopCleaner.UnitTests.Helpers
{
    /// <summary>
    /// Public static class that holds all of the 
    /// </summary>
    public static class SharedTestValues
    {
        #region Private Static Data Fields

        /// <summary>
        /// Private static data field that (poorly named really) gets access
        /// to the current directory for this application (for our purposes, during debugging, this
        /// will be the bin/debug folder, for demonstration only).
        /// </summary>
        private static Lazy<string> debugPath = new Lazy<string>(() => Directory.GetCurrentDirectory());

        #endregion Private Static Data Fields

        #region Public Constants

        /// <summary>
        /// Public constant that denotes the desktop path
        /// mock up AppSetting key (not actually real, faked
        /// by our MoqConfigurationHelper).
        /// </summary>
        public const string DesktopTestAppSettingKey = "DesktopPath";

        /// <summary>
        /// Public constant that denotes the desktop folder name 
        /// to use during Unit Testing.
        /// </summary>
        public const string TestDesktopFolder = "Test_Desktop";

        /// <summary>
        /// Public constant that denotes the text file folder name 
        /// to use during Unit Testing.
        /// </summary>
        public const string TestTxtFilesFolder = "Txt_Files";

        /// <summary>
        /// Public constant that denotes the sql file folder name 
        /// to use during Unit Testing.
        /// </summary>
        public const string TestSqlFilesFolder = "Sql_Files";

        /// <summary>
        /// Public constant that denotes the name of the first, test sql file 
        /// to use during Unit Testing.
        /// </summary>
        public const string TestSqlFileNameOne = "Test_Sql_File_One.sql";

        /// <summary>
        /// Public constant that denotes the name of the second, test sql file 
        /// to use during Unit Testing.
        /// </summary>
        public const string TestSqlFileNameTwo = "Test_Sql_File_Two.sql";

        /// <summary>
        /// Public constant that denotes the name of the first, test text file 
        /// to use during Unit Testing.
        /// </summary>
        public const string TestTxtFileNameOne = "Test_Txt_File_One.txt";

        /// <summary>
        /// Public constant that denotes the name of the second, test text file 
        /// to use during Unit Testing. 
        /// </summary>
        public const string TestTxtFileNameTwo = "Test_Txt_File_Two.txt";

        #endregion Public Constants

        #region Public Static Properties

        /// <summary>
        /// Gets access (static) to the lazy debugPath.value.
        /// </summary>
        public static string DebugPath
        {
            get
            {
                return debugPath.Value;
            }
        }

        #endregion Public Static Properties
    }
}
using DesktopCleaner.Interfaces;
using DesktopCleaner.UnitTests.Helpers;
using System.Collections.Specialized;
using System.IO;
using System.Linq;

namespace DesktopCleaner.UnitTests.MoqObject
{
    /// <summary>
    /// MoqConfigurationHelper class to provide a specific implementation of an
    /// IConfigurationHelper just for Unit Testing (not necessarily how best to structure
    /// a class, just for illustration purposes only).
    /// </summary>
    public class MoqConfigurationHelper : IConfigurationHelper
    {
        #region Private Static Data Fields

        /// <summary>
        /// Private static data field that represents a mock
        /// set of Application Settings for use during Unit Testing.
        /// </summary>
        private static NameValueCollection testAppSettingsCollection = null;

        #endregion Private Static Data Fields

        #region Constructor

        /// <summary>
        /// Initialises a new instance of a MoqConfigurationHelper.
        /// </summary>
        public MoqConfigurationHelper()
        {
            // THIS COULD, OF COURSE, BE STORED IN AN APP.CONFIG ALSO AS PART OF THIS PROJECT, BUT I WANT TO DEMONSTRATE A FULL MOCK UP HERE AND FANCY DOING A BIT MORE HARD WORK!

            // Set testAppSetingsCollection to be a new NameValueCollection
            testAppSettingsCollection = new NameValueCollection();

            // Create three, mock 'AppSettings' denoting a desktop, sql and text files path (to be used during Unit Tests)
            testAppSettingsCollection.Add(SharedTestValues.DesktopTestAppSettingKey, Path.Combine(SharedTestValues.DebugPath, SharedTestValues.TestDesktopFolder));
            testAppSettingsCollection.Add("Sql", Path.Combine(SharedTestValues.DebugPath, SharedTestValues.TestDesktopFolder, SharedTestValues.TestSqlFilesFolder));
            testAppSettingsCollection.Add("Txt", Path.Combine(SharedTestValues.DebugPath, SharedTestValues.TestDesktopFolder, SharedTestValues.TestTxtFilesFolder));
        }

        #endregion Constructor

        #region Public Expression Bodied Methods

        /// <summary>
        /// Public method that returns all 'AppSettings'
        /// (mock only) to the caller.
        /// </summary>
        /// <returns></returns>
        public NameValueCollection GetAllConfigurationSettings() => testAppSettingsCollection;

        /// <summary>
        /// Public method that returns a specified AppSetting
        /// to the caller based on key (mock only).
        /// </summary>
        /// <param name="key">The unique key for the value to be retrieved.</param>
        /// <returns>The value corresponding to the unique key.</returns>
        public string GetConfigurationSetting(string key) => testAppSettingsCollection.GetValues(key).FirstOrDefault();

        #endregion Public Expression Bodied Methods
    }
}

Being a stickler for providing full details, here is the class diagram for the Unit Test project:

Desktop Cleaner unit test project class diagram illustrating structure.

Desktop Cleaner Unit Test Project Class Diagram.

As a final thought, here’s a little bit of proof captured at runtime showing the different concrete implementations retrieved when hitting a breakpoint during debug of the application running in and out of a unit testing scenario:

Illustration showing the IConfigurationHelper object type during debug.

IConfigurationHelper Object Type During Debug.

Illustration showing the IConfigurationHelper object type during unit testing.

IConfigurationHelper Object Type During Unit Testing.

Since generating content for this post I’ve had more thoughts and ideas surrounding DI in general, something that seems to make a follow-up post a worthy thing to pursue! I’d like to flesh these examples out at the very least, so watch this space for more on this subject.

Thanks again all, until the next time…

Code Project: Epic Quest! Unit Testing

A staple of TDD (Test Driven Development), although not quite how I’ve implemented it here (as I already have too much code hanging around prior to writing any tests), the humble Unit Test is something I’ve wanted to dedicate a little bit of time to for a while; so here it is.

Inclusion of Unit Tests in a project, from my own personal experience, is something that rarely takes as much precedence as it should. Sometimes they aren’t included at all; other times they are incredibly slim-line to the point of only scratching the surface – This feels like a bit of a shame. I won’t get into my own feelings too much on the matter beyond saying that I’m of the belief that they certainly have a place and add value.

To this end I’ve gone ahead and created a few simple Unit Tests covering some of the foundation classes of the EpicQuest code base as follows:

  • DungeonTests test class – Covering the EpicDungeon class.
  • RoomTests test class – Covering the Room class (and creation of room content).
  • CombatTests test class – Covering a very basic combat scenario which will be fleshed out further in due course.
  • WeaponTests test class – Covering a basic Weapon object creation scenario.
  • ArmourTests test class – Covering a basic Armour object creation scenario.
  • TreasureTests test class – Currently unimplemented (the concept of ‘treasure’ still needs consideration).

As a bit of a visual aid, here’s a snippet from Visual Studio showing the Unit Test project structure and a Class Diagram illustrating the setup:

Unit Test Project Configuration.

Unit Test Project Configuration.

Unit Test Class Diagram.

Unit Test Class Diagram.

One of key things to notice here is that the EpicQuest project has been referenced in order for us to get hold of the types we need to test. A key benefit I’ve gained since inclusion of the tests is that I’ve been forced to compartmentalise my code further to make testing distinct pieces of functionality possible. Some code has also been re-homed fully (i.e. whereby I had methods sitting within classes whereby it didn’t actually make sense for them to live there from a code structure point of view and, therefore, a Unit Testing perspective). A few difficult questions have also been posed in relation to the accessibility of certain members during creation of the test code; issues I wouldn’t have had to consider without travelling down this path. All in all, pretty positive outcomes so far.

So what’s the anatomy of a basic Unit Test, how is it triggered and how can we glean valuable information from the results?

Within Visual Studio at least, the first requirement is a Unit Test project, as the previous screen shot illustrates (EpicQuest.Tests). From here, a great place to begin is by adding a Basic Unit Test template class:

Add New Unit Test Dialog.

Add New Unit Test Dialog.

This will provide the following stub class adorned with the TestClass attribute with a single test method, suitably adorned with the TestMethod attribute. These attributes identify the object/members as test entities for exposure in the Test Explorer window which we’ll see later.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EpicQuest.Tests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {

        }
    }
}

Test methods, regardless of the functionality being tested, will normally follow a structure that looks like this:

/// <summary>
/// Test method that ensures that the EpicDungeon
/// instance level RemoveDungeonRoom method is working
/// as expected.
/// </summary>
[TestMethod]
public void EpicDungeonRoomRemovalTest()
{
	//Phase one - Arrange (essentially complete any configuration to actually perform the test - In this case 
	//create a dungeon and add a known room instance to it, taking note of the room count prior to removal of the room)
	Room roomToRemove = new Room();

	EpicDungeon dungeon = new EpicDungeon(EpicDungeon.GenerateRandomRoomConfiguration()); //Opted to create a random room configuration to strengthen the test scenario
	dungeon.AddDungeonRoom(roomToRemove);

	int previousRoomCount = dungeon.DungeonRooms.Count;

	//Phase two - Test/Act (remove the room)
	dungeon.RemoveDungeonRoom(roomToRemove);

	//Phase three - Test/Assert (run tests to ensure that the correct end result, after removal of the room, has been achieved)
	Assert.IsFalse(dungeon.DungeonRooms.Contains(roomToRemove), "EpicDungeonRoomRemovalTest (DungeonTests) failed. Test room roomToRemove still found in dungeon.DungeonRooms.");
	Assert.AreEqual<int>((previousRoomCount - 1), dungeon.DungeonRooms.Count, "EpicDungeonRoomRemovalTest (DungeonTests) failed. Room count (dungeon.DungeonRooms.Count) has an unexpected value.");
}

The structure factors in three distinct phases:

  • Arrange. This stage involves configuring any objects and setup as required by the test you plan to perform.
  • Act. Post configuration you actually need to call the functionality you wish to test.
  • Test. Lastly, assertions are run in order to see if the code under test worked as expected.

The MSDN documentation surrounding running Unit Tests through Visual Studio is well written and quite comprehensive. Further information can be found by accessing the following link:

Walkthrough: Creating and Running Unit Tests for Managed Code.

In order to see if the code under test is working as expected the static members of the Assert class come into play (of which there are a few, see the Assert Class MSDN documentation for a full listing).

In the above listing, I’ve opted to firstly use Assert.IsFalse to make a logical test to see if the Room instance I am expecting to be removed by the RemoveDungeonRoom method has actually be taken out of the EpicDungeon instance. Lastly, I’ve called on a generic version (which is awesome!) of Assert.AreEqual to make a secondary check to ensure the remaining count of Room objects in the EpicDungeon is as I would expect, following the previous Room object removal. The Assert static members allow for value types and objects to be passed in for inspection and, in the instances I’ve seen, for a string message to be specified; to be shown if the test fails. You’ll see other examples of Assert methods in action, such as the Assert.IsInstanceOfType, in later examples.

The Assert class and the members on offer are fairly intuitive right off the bat; you’ll probably only need a few minor peeks at the documentation to get underway if you’re familiar with C#.

NOTE: Some of the xml comments and variable names, etc have been tweaked throughout the code at varying times meaning some of the screen shots you see may fall out of line slightly with the code samples. If something is functionally different then I have gone back and altered the screen shots to align with the code.

As there’s not tonnes of code as of yet in the tests I’ve written here’s the full listing of what I have so far:

Dungeon Tests

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EpicQuest.Models.Dungeon;
using System.Collections.Generic;

namespace EpicQuest.Tests.Dungeon
{
    /// <summary>
    /// Dungeon entity specific Unit Tests
    /// for the Epic Quest game.
    /// </summary>
    [TestClass]
    public class DungeonTests
    {
        /// <summary>
        /// Test method that ensures that the EpicDungeon
        /// object (static) level GenerateRandomRoomConfiguration method 
        /// is working as expected. 
        /// </summary>
        [TestMethod]
        public void EpicDungeonRoomGenerationTest()
        {
            //Generate a random room amount seed and get an actual number of rooms as generated by EpicDungeon.GenerateRandomRoomConfiguration. Obtain the difference
            int roomNumberSeed = new Random().Next(3, 10), roomNumber = EpicDungeon.GenerateRandomRoomConfiguration(roomNumberSeed).Count,
                difference = (roomNumber - roomNumberSeed);

            //Extra debugging
            System.Diagnostics.Trace.WriteLine(string.Format("EpicDungeonRoomGenerationTest roomNumberSeed {0}; roomNumber: {1}; difference: {2}.", roomNumberSeed, roomNumber, difference));

            //The test is successful if the difference is between 0 and 2 (these are the rules that I've specified)
            if (difference < 0 || difference > 2)
            {
                Assert.Fail("EpicDungeonRoomGenerationTest (DungeonTests) failed. Room difference between seed number and actual, generated number out of range. Difference: {0}", difference);
            }
        }

        /// <summary>
        /// Test method that ensures that the EpicDungeon
        /// object constructor is working
        /// as expected.
        /// </summary>
        [TestMethod]
        public void EpicDungeonCreationTestOne()
        {
            //Get a predefined list of rooms and pass them into an EpicDungeon object constructor
            List<Room> testRooms = new List<Room>();
            testRooms.AddRange(new Room[3] { new Room(), new Room(), new Room() });

            EpicDungeon dungeon = new EpicDungeon(testRooms);

            //Test to ensure that dungeon.DungeonRooms is based on our testRooms collection
            Assert.AreEqual(testRooms, dungeon.DungeonRooms, "EpicDungeonCreationTestOne (DungeonTests) failed. Test room array is not represented by dungeon.DungeonRooms (different refs).");
        }

        /// <summary>
        /// Test method that ensures that the EpicDungeon
        /// object constructor is working
        /// as expected (very basic test).
        /// </summary>
        [TestMethod]
        public void EpicDungeonCreationTestTwoBasic()
        {
            //Test the constructor that takes an array of room objects
            Room[] roomArray = new Room[] { new Room(), new Room(), new Room() };

            EpicDungeon dungeon = new EpicDungeon(roomArray);

            //The room array is converted into a list within the Room class - Do a simple count check as a follow up to EpicDungeonCreationTestOne
            Assert.AreEqual<int>(roomArray.Length, dungeon.DungeonRooms.Count, "EpicDungeonCreationTestTwoBasic (DungeonTests) failed. Test array room count differs to dungeon.DungeonRooms.Count.");
        }

        /// <summary>
        /// Test method that ensures that the EpicDungeon
        /// instance level AddDungeonRoom method is working
        /// as expected.
        /// </summary>
        [TestMethod]
        public void EpicDungeonRoomAdditionTest()
        {
            EpicDungeon dungeon = new EpicDungeon(EpicDungeon.GenerateRandomRoomConfiguration());
            
            int previousRoomCount = dungeon.DungeonRooms.Count;

            Room roomToAdd = new Room();
            dungeon.AddDungeonRoom(roomToAdd);

            Assert.IsTrue(dungeon.DungeonRooms.Contains(roomToAdd), "EpicDungeonRoomAdditionTest (DungeonTests) failed. Test Room roomToAdd not found in dungeon.DungeonRooms.");
            Assert.AreEqual<int>((previousRoomCount + 1), dungeon.DungeonRooms.Count, "EpicDungeonRoomAdditionTest (DungeonTests) failed. dungeon.DungeonRooms.Count has an unexpected value.");
        }

        /// <summary>
        /// Test method that ensures that the EpicDungeon
        /// instance level RemoveDungeonRoom method is working
        /// as expected.
        /// </summary>
        [TestMethod]
        public void EpicDungeonRoomRemovalTest()
        {
            //Phase one - Arrange (essentially complete any configuration to actually perform the test - In this case 
            //create a dungeon and add a known room instance to it, taking note of the room count prior to removal of the room)
            Room roomToRemove = new Room();

            EpicDungeon dungeon = new EpicDungeon(EpicDungeon.GenerateRandomRoomConfiguration()); //Opted to create a random room configuration to strengthen the test scenario
            dungeon.AddDungeonRoom(roomToRemove);

            int previousRoomCount = dungeon.DungeonRooms.Count;

            //Phase two - Test/Act (remove the room)
            dungeon.RemoveDungeonRoom(roomToRemove);

            //Phase three - Test/Assert (run tests to ensure that the correct end result, after removal of the room, has been achieved)
            Assert.IsFalse(dungeon.DungeonRooms.Contains(roomToRemove), "EpicDungeonRoomRemovalTest (DungeonTests) failed. Test room roomToRemove still found in dungeon.DungeonRooms.");
            Assert.AreEqual<int>((previousRoomCount - 1), dungeon.DungeonRooms.Count, "EpicDungeonRoomRemovalTest (DungeonTests) failed. Room count (dungeon.DungeonRooms.Count) has an unexpected value.");
        }
    }
}

Room Tests

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EpicQuest.Models.Dungeon;
using EpicQuest.Interfaces;
using EpicQuest.Models;
using System.Collections.Generic;
using System.Linq;
using EpicQuest.Models.Items;

namespace EpicQuest.Tests.Rooms
{
    /// <summary>
    /// Room entity specific Unit Tests
    /// for the Epic Quest game.
    /// </summary>
    [TestClass]
    public class RoomTests
    {
        /// <summary>
        /// Test method that ensures that the Room
        /// object (static) level GenerateRandomRoomContent method 
        /// is working as expected. 
        /// </summary>
        [TestMethod]
        public void RoomContentGenerationTest()
        {
            //Test that Room.GenerateRandomRoomContent is producing content based on the seed provided
            int roomContentSeed = 2, roomContentNumber = Room.GenerateRandomRoomContent(roomContentSeed).Count, difference = (roomContentNumber - roomContentSeed);

            //Produce some extra debugging output
            System.Diagnostics.Trace.WriteLine(string.Format("RoomContentGenerationTest (RoomTests) roomContentSeed {0}; roomContentNumber: {1}; difference: {2}.", 
                roomContentSeed, roomContentNumber, difference));

            //Ensure the room content number produced is within the expected range (based on the seed)
            if (difference < 0 || difference > 2)
            {
                Assert.Fail("RoomContentGenerationTest (RoomTests) failed. Room content difference between seed number and actual, generated number out of range. Difference: {0}", difference);
            }
        }

        /// <summary>
        /// Test method that ensures that the Room
        /// object constructor is working as expected.
        /// </summary>
        [TestMethod]
        public void RoomContentCreationTestOne()
        {
            //Add some content to a list of type IRoomContent and pass this to the Room constructor
            List<IRoomContent> roomContentList = new List<IRoomContent>();
            roomContentList.AddRange(new IRoomContent[] { new Skeleton(), new Skeleton(), new Kobold(), new Kobold() });

            Room room = new Room(roomContentList);

            //Check to ensure that the content in the newly created Room object is based on the information passed to the constructor
            Assert.AreEqual(roomContentList, room.RoomFeatures, "RoomContentCreationTestOne (RoomTests) failed. Test room content array is not represented by room.RoomFeatures (different refs).");
        }

        /// <summary>
        /// Test method that ensures that the Room
        /// object constructor is working
        /// as expected (very basic test).
        /// </summary>
        [TestMethod]
        public void RoomContentCreationTestTwoBasic()
        {
            //Chuck an array of IRoomContent to the Room constructor
            IRoomContent[] roomContent = new IRoomContent[] { new Kobold(), new Skeleton() };

            Room room = new Room(roomContent);

            //Again, similar to the Dungeon test of a similiar nature, do a simple count of content items and ensure it's correct
            Assert.AreEqual<int>(roomContent.Length, room.RoomFeatures.Count, "RoomContentCreationTestTwoBasic (RoomTests) failed. Test array room Content count differs to room.RoomFeatures.Count.");
        }

        /// <summary>
        /// Test method that ensures that the Room
        /// instance level AddContentToRoom method is working
        /// as expected.
        /// </summary>
        [TestMethod]
        public void RoomContentAdditionTest()
        {
            //Create a new room and added random content (as base content). Take a note of the items of content currently in the room
            Room room = new Room(Room.GenerateRandomRoomContent());

            int currentRoomFeatureCount = room.RoomFeatures.Count;

            //Add a new monster to the room
            IRoomContent kobold = new Kobold(); 
            room.AddContentToRoom(kobold);

            //Ensure the monster has correctly been added to the room
            Assert.IsTrue(room.RoomFeatures.Contains(kobold), "RoomContentAdditionTest (RoomTests) failed. Test room feature kobold not found in room.RoomFeatures.");
            Assert.AreEqual<int>((currentRoomFeatureCount + 1), room.RoomFeatures.Count, "RoomContentAdditionTest (RoomTests) failed. Room feature count (room.RoomFeatures.Count) has an unexpected value.");
        }

        /// <summary>
        /// Test method that ensures that the Room
        /// instance level RemoveContentFromRoom method is working
        /// as expected. 
        /// </summary>
        [TestMethod]
        public void RoomContentRemovalTest()
        {
            //Add a new skeleton to a new room (with some other random content to strengthen the test example). Take a note of the count of room features prior to calling RemoveContentFromRoom
            IRoomContent skeleton = new Skeleton();

            Room room = new Room(Room.GenerateRandomRoomContent());
            room.AddContentToRoom(skeleton);

            int previousRoomFeatureCount = room.RoomFeatures.Count;

            //Run the test, remove the skeleton from the room
            room.RemoveContentFromRoom(skeleton);

            //Ensure that the known skeleton instance has been removed from the room (and that the count of remaining pieces of room content is correct)
            Assert.IsFalse(room.RoomFeatures.Contains(skeleton), "RoomContentRemovalTest (RoomTests) failed. Test room feature skeleton still found in room.RoomFeatures.");
            Assert.AreEqual<int>((previousRoomFeatureCount - 1), room.RoomFeatures.Count, "RoomContentRemovalTest (RoomTests) failed. Room feature count (room.RoomFeatures.Count) has an unexpected value.");
        }

        /// <summary>
        /// Test method that ensures that the Room
        /// instance level RoomClearedOfMonstersTest method 
        /// is working as expected. 
        /// </summary>
        [TestMethod]
        public void RoomClearedOfMonstersTest()
        {
            //Configure some fixed, known pieces of room content and add it to a new room
            IRoomContent skeleton = new Skeleton(), kobold = new Kobold(), treasureItem = new TreasureItem();

            Room room = new Room(skeleton, kobold, treasureItem);

            //Test that the room contains monsters at this point
            Assert.IsFalse(room.RoomClearedOfMonsters, "RoomClearedOfMonstersTest (RoomTests) failed. No monsters detected in the room.");

            //Remove just the fixed, known monsters - Leaving the treasure item intact
            room.RemoveContentFromRoom(skeleton, kobold);

            //Final round of tests. Ensure all monsters have been removed and that only one piece of content remains (the treasure item)
            Assert.IsTrue(room.RoomClearedOfMonsters, "RoomClearedOfMonstersTest (RoomTests) failed. Monsters detected in the room.");
            Assert.AreEqual<int>(room.RoomFeatures.Count, 1, "RoomClearedOfMonstersTest (RoomTests) failed. An unexpected room feature count was detected (room.RoomFeatures.Count).");
            Assert.IsInstanceOfType(room.RoomFeatures.First(), typeof(TreasureItem), "RoomClearedOfMonstersTest (RoomTests) failed. Remaining room feature is not of the expected type. Type detected: {0}",
                room.RoomFeatures.First().GetType().Name);
        }
    }
}
 

Combat Tests

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EpicQuest.Interfaces;
using EpicQuest.Models;
using EpicQuest.Utility;

namespace EpicQuest.Tests.Combat
{
    /// <summary>
    /// Combat based entity specific Unit Tests
    /// for the Epic Quest game. 
    /// </summary>
    [TestClass]
    public class CombatTests
    {
        /// <summary>
        /// Test method that ensures that the StaticGameActions
        /// object (static) level HandleCombat method 
        /// is working as expected.  
        /// </summary>
        [TestMethod]
        public void CombatTestOne()
        {
            //Setup the aggressor and defender ICombatant supporting objects
            ICombatant aggressor = new Brawler(), defender = new Skeleton();

            //Handle the combat. Take the defenders previous health and obtain the damage done from StaticGameActions.HandleCombat call
            int defenderPreviousHealth = defender.Health, damageDone = StaticGameActions.HandleCombat(aggressor, defender);

            //Test that the defenders health has been subtracted properly (TODO - Handle dead defenders and make sure this works if a defender is overkilled)
            Assert.AreEqual<int>((defenderPreviousHealth - damageDone), defender.Health);
        }
    }
}

Weapon Tests

using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EpicQuest.Models;
using EpicQuest.Models.Dice;

namespace EpicQuest.Tests.Items
{
    /// <summary>
    /// Weapon entity specific Unit Tests
    /// for the Epic Quest game. 
    /// </summary>
    [TestClass]
    public class WeaponTests
    {
        /// <summary>
        /// Test method that ensures that the Weapon
        /// object constructor is working as expected.
        /// </summary>
        [TestMethod]
        public void WeaponTestBasic()
        {
            //Create a new Weapon
            Weapon weaponItem = new Weapon(GameEnums.HeroOffensiveItemType.LegendaryBastardSword);

            //Ensure that the die associated with the object are as expected
            Assert.IsTrue((weaponItem.Dice.Where(die => die is BlueDie).Count() == 1 && weaponItem.Dice.Where(die => die is RedDie).Count() == 1
                && weaponItem.Dice.Count == 2), "WeaponTestBasic (WeaponTests) failed. Incorrect type of die/die count detected.");
        }
    }
}

Armour Tests

using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EpicQuest.Models;
using EpicQuest.Models.Dice;

namespace EpicQuest.Tests.Items
{
    /// <summary>
    /// Armour entity specific Unit Tests
    /// for the Epic Quest game. 
    /// </summary>
    [TestClass]
    public class ArmourTests
    {
        /// <summary>
        /// Test method that ensures that the Armour
        /// object constructor is working as expected. 
        /// </summary>
        [TestMethod]
        public void ArmourTestBasic()
        {
            //Die property is publically accessible??? TO CHECK; Either way create a new, known armour type
            Armour armourPiece = new Armour() { DefensiveItemType = GameEnums.HeroDefensiveItemType.LeatherChestPiece, DefensiveItemTypeSlot = GameEnums.HeroDefensiveItemSlotType.Chest };

            //Ensure the die for this piece of armour are correct
            Assert.IsTrue((armourPiece.Dice.Where(die => die is GreenDie).Count() == 2 && armourPiece.Dice.Count == 2), "ArmourTestBasic (ArmourTests) failed. Incorrect type of die/die count detected.");
        }
    }
}

Treasure Item Tests

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EpicQuest.Tests.Items
{
    /// <summary>
    /// TreasureItem entity specific Unit Tests
    /// for the Epic Quest game. 
    /// </summary>
    [TestClass]
    public class TreasureTests
    {
        /// <summary>
        /// TO DO.
        /// </summary>
        [TestMethod]
        public void TreasureTestBasic()
        {
            //TO DO, Treasure not fully implemented
        }     
    }
}

So how do you run these tests? It’s pretty damn easy – You just make use of the Test Explorer window in Visual Studio. A massive plus for me is that Unit Test projects, class templates and the Test Explorer are all available with the express edition of Visual Studio; big win! Either type ‘Test Explorer’ into the Quick Launch search bar in the top right hand corner of the Visual Studio UI or navigate to TEST > Windows > Test Explorer on the Visual Studio menu bar:

Accessing the Test Explorer Window.

Accessing the Test Explorer Window.

The Test Explorer window will automatically detect projects in scope that contain members marked with the TestMethod attribute (the classes of course need the TestClass attribute to). The Test Explorer window allows you to run all tests, previously failed tests, individual tests or a specific playlist of tests. The Test Explorer will appear like this in its initial state:

The Test Explorer Window (Initial State).

The Test Explorer Window (Initial State).

Here’s what the Test Explorer looks like after a successful test run. For starters, you get a run-down of how long each test took to run along with a link taking you to the location of the test method in the source code. The image shows an example of some additional debugging output:

Test Additional Output Example.

Test Additional Output Example.

In order to get hold of extra debugging output you can use the Trace.WriteLine static method as indicated in the following code snippet:

/// <summary>
/// Test method that ensures that the Room
/// object (static) level GenerateRandomRoomContent method 
/// is working as expected. 
/// </summary>
[TestMethod]
public void RoomContentGenerationTest()
{
	//Test that Room.GenerateRandomRoomContent is producing content based on the seed provided
	int roomContentSeed = 2, roomContentNumber = Room.GenerateRandomRoomContent(roomContentSeed).Count, difference = (roomContentNumber - roomContentSeed);

	//Produce some extra debugging output
	System.Diagnostics.Trace.WriteLine(string.Format("RoomContentGenerationTest (RoomTests) roomContentSeed {0}; roomContentNumber: {1}; difference: {2}.", 
		roomContentSeed, roomContentNumber, difference));

	//Ensure the room content number produced is within the expected range (based on the seed)
	if (difference < 0 || difference > 2)
	{
		Assert.Fail("RoomContentGenerationTest (RoomTests) failed. Room content difference between seed number and actual, generated number out of range. Difference: {0}", difference);
	}
}

Let’s rig a failure. I’ve simulated an underlying change to the code base that is going to trip up some of our Unit Tests. We receive the following output in the Test Explorer window after running all tests:

Test Explorer Failure Example.

Test Explorer Failure Example.

Two of our test methods fell over and a quick look at the output suggests that the Room class AddContentToRoom instance level method appears to be functioning unexpectedly (the room feature is not being added to the room). This is the state of the source code as it currently stands:

/// <summary>
/// Public method that allows a piece of content/multiple pieces
/// of content to be added to this particular Room.
/// </summary>
/// <param name="features">A single piece of content/array of content pieces to add to the Room.</param>
public void AddContentToRoom(params IRoomContent[] features)
{
	if (features != null && features.Count() > 0)
	{
		foreach (IRoomContent item in features)
		{
			roomFeatures.Add(new Kobold());
		}
	}
}

Looking through this code we can see that the method accepts an array of type IRoomContent named features. After a null check and a count to ensure the array is not empty we enter a simple foreach statement to add the IRoomContent supporting objects to the roomFeatures data field. The roomFeatures.Add statement is however simply adding a new Kobold object to the roomFeatures field list and ignoring the items in the array being passed in. Bad bug; slap on the wrists for introducing that!!! Let’s alter it to this:

/// <summary>
/// Public method that allows a piece of content/multiple pieces
/// of content to be added to this particular Room.
/// </summary>
/// <param name="features">A single piece of content/array of content pieces to add to the Room.</param>
public void AddContentToRoom(params IRoomContent[] features)
{
	if (features != null && features.Count() > 0)
	{
		foreach (IRoomContent item in features)
		{
			roomFeatures.Add(item);
		}
	}
}

Now we are using the foreach item variable correctly and adding this to the roomFeatures collection.

Back in the Test Explorer we are now able to explicitly re-run just the failed tests as shown:

Failed Tests in the Test Explorer Window.

Failed Tests in the Test Explorer Window.

The failed tests run up and we can see that these tests run without error, happy days!

Passed Tests in the Test Explorer Window.

Passed Tests in the Test Explorer Window.

As for Unit Testing, that’s about it for now. The only other small alteration I’ve made to the existing code base at this time is removal of the HandleCombat method from the GameManager class (to aid in Unit Testing/general code structure). This now resides in a new static class called StaticGameActions.

using System;
using EpicQuest.Interfaces;

namespace EpicQuest.Utility
{
    /// <summary>
    /// Public static utility class for the
    /// EpicDungeon game.
    /// </summary>
    public static class StaticGameActions
    {
        /// <summary>
        /// Private method that shows, in principle, how combat
        /// can be handled between two ICombatant based objects.
        /// </summary>
        /// <param name="aggressor">The instigator of the combat represented by a class implementing ICombatant.</param>
        /// <param name="target">The defender represented by a class implementing ICombatant.</param>
        public static int HandleCombat(ICombatant aggressor, ICombatant target)
        {
            //Prepare values representing the dice (random) and values to keep a tally of the offence, defence and difference totals
            int damageTotal = 0, defenceTotal = 0, difference = 0;
            Random rollDie = new Random();

            //Roll attack dice for the aggressor and get a damage total
            aggressor.AttackDice.ForEach(attackDie =>
            {
                damageTotal += (byte)attackDie.DieFaces[rollDie.Next(0, 5)];
            });

            //Roll defence dice for the defencder and get a defence total
            target.DefenceDice.ForEach(defenceDie =>
            {
                defenceTotal += (byte)defenceDie.DieFaces[rollDie.Next(0, 5)];
            });

            //Determine the difference between the damage/defence totals and deal damage to the target if required
            difference = damageTotal - defenceTotal;

            if (difference > 0)
            {
                target.Health -= difference;
            }

            return difference < 0 ? 0 : difference; //Only return non-negative results
        }
    }
}

That’s about it folks for the time being. Unit Testing is an interesting topic that requires some further investigation so I want to bring you another small piece looking at the TestInitialize and TestCleanup attributes and how more in depth tests can be constructed.

In other news, I’ve upgraded my blog account and got a new domain which I’m sure you’ll notice (splashed some cash as promised!). I’ll shortly be, as stated, making a few styling tweaks to the theme in addition to adding a dedicated Facebook page and Twitter feed, so watch this space.

Ciao for now.