Posts tagged ‘Unit Test’

Following on from my previous post I thought it only fair to dive down into the code that was shown in that article and give a bit more of an explanation on what is going on.

First of all, some of you may have looked at the sample code and asked “What is the ContextSpecification class that the ShoppingCartContext is derived from?” – fair question and one that deserves to be explained.

Allow me to digress momentarily in order to try and set the context (for you seasoned BDD’rs – pun intended!). I was first introduced to BDD (indirectly) at a session on coding Katas at TechEd 2010 in New Orleans and BDD was not exactly why I had attended the session. The Behavior Driven aspect of the interactive presentation was very brief and at the time I was left with the desire to know more, so before the End of TechEd party I spent time scouring numerous technical and not so technical sites – looking for further details. What I found was far more than I had bargained for; it wasn’t that the tests were written in a natural language sort of way, it was the idea that the code was being driven via tests that were in turn driven by use cases – OMG!!!! You mean that the requirements analyst directly affects the way the tests are written? I had already bought in to the TDD mindset and to me it had made perfect sense, but this was far beyond what I had bargained for. So in true engineering fashion – I tried it out myself and low and behold it worked (albeit in a very sanitized and benign sort of way). Before we go any further, I would highly recommend that you read Dan North’s Introduction to BDD (http://behaviour-driven.org/Introduction); there you will see that many of us may be fortunate to get to step 4 of the steps to enlightenment. In my case I was there and thanks to the intro to BDD I would get to 6 and hopefully on to step 7.

So there I am trying to implement my first set of BDD tests (before code implementation) and I hit my first stumbling block. The Context Specification Framework I had been introduced to at TechEd, Machine.Specification by Aaron Jensen (http://github.com/machine/machine.specifications), had wonderful plugin capabilities for ReSharper but nothing that I could use inside of MSTest; yes I know, “big deal”, “so what”, blah, blah… Well as much as I love ReSharper, I still prefer to run MSTest as it is what is run in my build environments. Therefore anything that I have difficulty running with MSTest will be an even bigger hassle on my build machines. So off I went again, looking for more jewels in the proverbial rain-forest of information and lo-and-behold I came across Eric Lee’s implementation of a base class that uses MSpec (http://blogs.msdn.com/b/elee/archive/2009/01/20/bdd-with-mstest.aspx) called ContextSpecification. It is a superb abstract class that allows me to easily write my BDD style test classes and thereby drive my code implementation.

Now then, we have the MSpec assembly reference and now an abstract class (ContextSpecification) to derive from and as I am a big fan of RhinoMocks (for generating mocks or stubs), let’s throw that into the mix. With this base we are ready to go do some BDD.

As I have stated before, the Requirements are what drive our tests in BDD; more specifically the creation of Use Cases will drive the tests. So going back to the example in my previous post, we will start with the following requirement:

    A customer can add items to their shopping cart.

Strictly speaking, in SCRUM terms, this should be written as follows:

    AS A Customer

    I WANT to be able to add items to a shopping cart

    SO THAT I can keep a collection of items that I want to buy.

From this user story we can expand the Use Cases (I also consider them as Conditions of Acceptance for the User Story) to give me detail to the high level statement:

Scenario 1: Adding items to an empty shopping cart

  • GIVEN that the shopping cart is empty
  • WHEN the customer adds 1 item
  • THEN the shopping cart should contain 1 item

Scenario 2: Adding items to a full shopping cart

  • GIVEN that the shopping cart contains 2 items
  • WHEN the customer adds 1 item
  • THEN the shopping cart should contain the 2 existing items and the 1 new item.

I have purposefully capitalized the GIVEN, WHEN and THEN, simply to draw parallels with the AS A <role> I WANT <functionality> SO THAT <benefit> of SCRUM and hopefully highlight the fact that patterns can be drawn from both and thereby help us to create consistent ways in which to write both User Stories and Use Cases.

Now that we have our Use Cases we can start to create our tests. Taking the first scenario we will need to setup a Shopping cart context; which is where we bring in the ContextSpecification abstract class (remember, we don’t have any implementation code yet):

namespace Bdd.Shopping
{
    public class ShoppingCartContext : ContextSpecification
    {
        /// <summary>
        /// The "Given some initial context" method
        /// </summary>
        protected override void Context()
        {
            // setup your class under test
        }
    }

    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1 : ShoppingCartContext
    {
        /// <summary>
        /// The "When an event occurs" method
        /// </summary>
        protected override void BecauseOf()
        {
            //
            // TODO: Add behavior setup (Action) here
            //
        }

        /// <summary>
        /// The "then ensure some outcome" method.
        /// </summary>
        [TestMethod]
        public void TestMethod1()
        {
            //
            // TODO: Add test logic here
            //
        }
    }
}

Remember that the use case was written as follows:

  • GIVEN that the shopping cart is empty
  • WHEN the customer adds 1 item
  • THEN the shopping cart should contain 1 item

The “GIVEN” aspect of the use case is the Context() method of our base class – where we initialize our member variables.

The “WHEN” of the use case will be the TestClass – this will be the container for the THEN aspect.

The “THEN” will be the TestMethod and perform the appropriate assert(s).

Putting all of this together we arrive at the following:

namespace Bdd.Shopping
{
    public class ShoppingCartContext : ContextSpecification
    {
        protected ShoppingCart _cart;
    }

    /// <summary>
    /// Test Class for "WHEN the customer adds 1 item THEN the shopping cart should contain 1 item"
    /// use case.
    /// </summary>
    [TestClass]
    public class when_1_item_is_added_to_an_empty_shopping_cart : ShoppingCartContext
    {
        /// <summary>
        /// The "Given some initial context" method
        /// </summary>
        protected override void Context()
        {
            _cart = new ShoppingCart();
        }

        /// <summary>
        /// The "When an event occurs" method
        /// </summary>
        protected override void BecauseOf()
        {
            ShoppingItem item = new ShoppingItem();
            _cart.Add(item);
        }

        /// <summary>
        /// The "then ensure some outcome" method.
        /// </summary>
        [TestMethod]
        public void then_the_shopping_cart_should_contain_1_item()
        {
            Assert.AreEqual(1, _cart.Items.Count);
        }
    }
}

The class name of the TestClass and the method name of the TestMethod help us associate the test with the first use case; “WHEN the customer adds 1 item THEN the shopping cart should contain 1 item”.

In true TDD fashion we have only implemented enough to compile (i.e. absolute minimum of implementation classes) – so this test will fail:

BDD  - Fail First

The next step, of course is to put in the functionality to get the test to pass (TDD mantra – Red, Green, Refactor, Repeat until all test are done). In the case of the implementation classes, we will go from this:

    public class ShoppingCart
    {
        internal void Add(ShoppingItem item)
        {
            throw new NotImplementedException();
        }

        public List<ShoppingItem> Items { get; set; }
    }

    public class ShoppingItem
    {
        public ShoppingItem() {}
    }

To this:

    public class ShoppingCart
    {
        public ShoppingCart()
        {
            Items = new List<ShoppingItem>();
        }

        internal void Add(ShoppingItem item)
        {
            Items.Add(item);
        }

        public List<ShoppingItem> Items { get; set; }
    }

    public class ShoppingItem
    {
        public ShoppingItem() {}
    }

This will give us green in our first test:

BDD  - Then Pass

Now we move on to the next use case:

  • GIVEN that the shopping cart contains 2 items
  • WHEN the customer adds 1 item
  • THEN the shopping cart should contain the 2 existing items and the 1 new item.

Here we see that the Use Case is more specific in that it is requiring us to check that the latest item has been added – we will need to add an identifier to the item be able to check against it in our assertion. This will mean that we will need to run our first test again as we will be changing the implementation classes.

First we write the new Test class and associated Test method and do the minimum implementation to get it to compile.

New Test class and Test method:

    [TestClass]
    public class when_1_item_is_added_to_a_cart_containing_2_items : ShoppingCartContext
    {
        private const string NewItemTitle = "Test Driven Development By Kent Beck";

        /// <summary>
        /// The "Given some initial context" method
        /// </summary>
        protected override void Context()
        {
            _cart = new ShoppingCart();
            ShoppingItem item = new ShoppingItem("Behavior Driven Development By Dan North");
            _cart.Add(item);
            item = new ShoppingItem("Agile Software Development with Scrum By Ken Schwaber and Mike Beedle");
            _cart.Add(item);
        }

        /// <summary>
        /// The "When an event occurs" method
        /// </summary>
        protected override void BecauseOf()
        {
            ShoppingItem item = new ShoppingItem(NewItemTitle);
            _cart.Add(item);
        }

        /// <summary>
        /// The "then ensure some outcome" method.
        /// </summary>
        [TestMethod]
        public void then_the_shopping_cart_should_contain_the_2_existing_items_and_the_1_new_item()
        {
            Assert.AreEqual(3, _cart.Items.Count);
            Assert.AreEqual(NewItemTitle, _cart.Items[2].Title);
        }
    }

Minimum implementation classes:

    public class ShoppingCart
    {
        public ShoppingCart()
        {
            Items = new List<ShoppingItem>();
        }

        internal void Add(ShoppingItem item)
        {
            Items.Add(item);
        }

        public List<ShoppingItem> Items { get; set; }
    }

    public class ShoppingItem
    {
        public ShoppingItem(string title) {}

        public string Title { get; set; }
    }

When we run the test, the first one will still pass, but seeing as we have done the minimum to be able to compile the second test will fail:

BDD  - Then Fail Again

So therefore the next stage is to make the red go green and “back fill” the correct functionality – to update the implementation class as follows:

    public class ShoppingItem
    {
        public ShoppingItem(string title)
        {
            Title = title;
        }

        public string Title { get; set; }
    }

Now when we run the tests all should be green:

BDD  - Then Pass Again

As we continue we are always aligning our code with the use cases and therefore ensuring that we are adding the business value as intended. With the TDD aspect we are also ensuring that any changes we do to the implementation can be done so with the peace of mind that as long as all of the tests past before we go on to the next use case, we will be building robust code.

As I have said in this blog post and the previous one, BDD is a natural evolution of TDD in as much that it adds the linkage from the tests to the requirements. At a personal level it is also a natural progression in my continuous evolution as a software professional and it helps to consolidate my understanding of the requirements more than any type of review ever could.

Make no mistake, BDD and (especially) TDD are a definite shift in mindset mostly in the idea that the test is written first and you only write enough of the implementation to be able to get the test to pass. This puts the onus on the test being right in the first instance and places more significance on the testing aspect of software development.

  • Share/Bookmark

Although many people may have commented so. I have been drawn in and fired up by Behavior Driven Development (BDD) and through various discussions I have found a great number of non-believers and nay sayers who, strangely enough, don’t truly “get” Agile development either. Strange that, isn’t it? The brick wall I sometimes encounter when trying to get the idea of SCRUM across to certain people is that it is “academic and won’t really work for us”, “we can use facets of it, but the thing as a whole just doesn’t fit”. And in a similar way the same arguments came up when I mention BDD.

So let’s refresh our knowledge on BDD before we go any further. Behavior Driven Development was the brain child of Dan North and he has an excellent article about its evolution on his blog (http://blog.dannorth.net/introducing-bdd/). BDD brings together the Requirements Analyst with the Testers and Developers to start breaking down the stories into use cases (may be known as Conditions of Acceptance in a User Story). So what’s new you may ask? Well, those Use Cases, written in natural language become the definitions of the Test Cases and Unit Tests that will drive the code.

What? Test before Code? Unheard of! Charlatan!

Oh please! Really? Yes folks, BDD is sometimes seen as an extension of TDD (Test Driven Development). TDD pushes the testing to the fore and is used to drive the implementation, so rather than waiting until the code is written to create the unit tests, we now create the unit test and write the code to make the test pass. In my mind I see BDD as TDD on steroids! So how does it work?

Imagine the following (benign and fictitious) requirement:

  • A customer can add items to their shopping cart.

This could be expanded into the following use cases (or conditions of acceptance):

Scenario 1: Adding items to an empty shopping cart

  • Given that the shopping cart is empty
  • When the customer adds 1 item
  • Then the shopping cart should contain 1 item

Scenario 2: Adding items to a full shopping cart

  • Given that the shopping cart contains 2 items
  • When the customer adds 2 items
  • Then the shopping cart should contain 4 items

From this we can derive our test cases and the definition of our Unit Tests. Consider the following code:

namespace ShoppingCartExample
{
    public class ShoppingCartContext : ContextSpecification
    {
        protected ShoppingCart _cart;
    }

    [TestClass]
    public class when_1_item_is_added_to_an_empty_shopping_cart : ShoppingCartContext
    {
        ///

        /// The "Given some initial context" method
        /// 

        protected override void Context()
        {
            _cart = new ShoppingCart();

        }

        ///

        /// The "When an event occurs" method
        /// 

        protected override void BecauseOf()
        {
            ShoppingItem item = new ShoppingItem("Behavior Driven Development By Dan North");
            _cart.Add(item);

        }

        ///

        /// The "then ensure some outcome" method.
        /// 

        [TestMethod]
        public void the_shopping_cart_should_contain_1_item()
        {
            Assert.AreEqual(1, _cart.Items.Count);
        }
    }

    [TestClass]
    public class when_2_items_are_added_to_a_shopping_cart_containing_two_items : ShoppingCartContext
    {
        ///

        /// The "Given some initial context" method
        /// 

        protected override void Context()
        {
            _cart = new ShoppingCart();
            ShoppingItem item = new ShoppingItem("Behavior Driven Development By Dan North");
            _cart.Add(item);
            item = new ShoppingItem("Agile Software Development with Scrum By Ken Schwaber and Mike Beedle");
            _cart.Add(item);
        }

        ///

        /// The "When an event occurs" method
        /// 

        protected override void BecauseOf()
        {
            ShoppingItem item = new ShoppingItem("Test Driven Development By Kent Beck");
            _cart.Add(item);
            item = new ShoppingItem("The Art of Unit Testing By Roy Osherove");
            _cart.Add(item);
        }

        ///

        /// The "then ensure some outcome" method.
        /// 

        [TestMethod]
        public void the_shopping_cart_should_contain_4_item()
        {
            Assert.AreEqual(4, _cart.Items.Count);
        }
    }
}

I will go in to more detail about the code in a later blog article. For now we will concentrate on the naming of the classes and Test Methods.

Once we have implemented the code that the tests call, we will see the following results:

BDD Unit Test Results

You can now see how the unit test classes relate to the context of the use case (“Given that the shopping cart is empty” with “When the customer adds 1 item“) and the test methods relate to the result criteria (“Then the shopping cart should contain 1 item“).

The beauty of BDD is that it slams the team into instant interaction; there are no ceremonies, burndowns or roles to allow us to ease into the Agile environment; this is an “in your face”, “get on with it” approach and is an Agile technique that can easily be used within the SCRUM framework. This is also the “ugly” part of BDD, individuals may not be comfortable working in this way and this leads to a bigger question of a right fit for those individuals. The benefits of working introducing BDD to your team are obvious:

  • The clarity of requirements are brought to the fore faster than in any other circumstance – if you can’t write the test, then something is wrong with the requirements and the team will spot that as soon as they sit down to work with the requirements analyst.
  • Unit Tests are written from the beginning and make sense to everyone, even the non-technical members of the group can see how well the design is going, just by looking at the unit tests being run (or not).
  • The design is driven from the requirements – as it should be; why would you write code for anything else? Therefore we see a facet of lean.

The adoption of BDD is by no means easy and it can, as stated before, lead to exposing some deficiencies in the team or environment. This in itself may be a good incentive to adopt; hyper-productive teams are driven to succeed by the very nature of their ease in adopting agile techniques and BDD lends itself to those types of individuals.

  • Share/Bookmark

Finally! I’ve been itching to get into the latest incarnation of the EF for a while now and finally I had the opportunity to take it around the block (kick the tires and generally rough it up). My main interest in EF 2.0 was the supposed support for Unit Testing and the improvements on using POCO to map to the Data Model.

I have to admit, although I liked EF 1.0 when I first started using it, one of my biggest bug bears was the fact that you had to “disconnect” your entities before you could really work with them outside the context (no pun intended) of the Entity Framework. I was also quite miffed when I discovered there was no easy way to mock the underlying data layer so I ended up with code like this:


    /// <summary>
    /// Loads a customer instance with the relevant information from the database.
    /// </summary>
    /// <param name="i_customerId">The customerId of the customer data to be retrieved.</param>
    /// <param name="o_customer">The customer instance to be created.</param>
    public void Load(string i_customerId, out Customer o_customer)
    {
        if (string.IsNullOrEmpty(i_customerId))
        {
            throw new ArgumentException("Parameter cannot be null.", "i_customerId");
        }
        int numericVal;
        if (!int.TryParse(i_customerId, out numericVal))
        {
            throw new ArgumentException("Parameter cannot be non-numeric.", "i_customerId");
        }
        if (numericVal < 0 || numericVal > 9999)
        {
            throw new ArgumentOutOfRangeException("i_customerId");
        }

        m_customerRepository.Load(i_customerId, out o_customer);

        if (o_customer != null)
        {
            if (!string.IsNullOrEmpty(o_customer.ContactName) && o_customer.ContactName.Contains(" "))
            {
                o_customer.ContactName = o_customer.ContactName.Trim(' ');
                string[] names = o_customer.ContactName.Split(' ');
                if (names.Length > 1)
                {
                    names[names.Length - 1] = names[names.Length - 1].ToUpper();
                }
                o_customer.ContactName = string.Join(" ", names);
            }
        }
    }

In this case, m_customerRepository is the injected ICustomerRepository instance. When we look at the implementation of the actual data layer class (which does not get tested by the Unit Test, we find that inside the Load method we have the following:


    /// <summary>
    /// Loads a customer instance with the relevant information from the database.
    /// </summary>
    /// <param name="i_customerId">The customerId of the customer data to be retrieved.</param>
    /// <param name="o_customer">The customer instance to be created.</param>
    public void Load(string i_customerId, out BaseCustomer o_customer)
    {
        Customer customer = (CustomerSet.Where(cust => !string.IsNullOrEmpty(cust.CustomerID) &&
                                                       cust.CustomerID == i_customerId)).First();
        if (customer != null)
        {
            o_customer = new BaseCustomer()
                             {
                                 Address = customer.Address,
                                 City = customer.City,
                                 CompanyName = customer.CompanyName,
                                 ContactName = customer.ContactName,
                                 ContactTitle = customer.ContactTitle,
                                 Country = customer.Country,
                                 CustomerID = customer.CustomerID,
                                 Fax = customer.Fax,
                                 Phone = customer.Phone,
                                 PostalCode = customer.PostalCode,
                                 Region = customer.Region
                             };
        }
        else
        {
            o_customer = null;
        }
    }

Not the best way of doing things, that is for sure! In fact it is downright ugly (IMHO). So when I heard that there were improvements to the EF for .NET 4.0, especially in the area of Unit Testing I was curious to say the least. As I delved in deeper I started finding more and more things that made it more attractive to my style of development. For example one of the beauties of EF 2.0 is the fact that you can remove the CustomTool that generates the entity classes that are bound to the data model (through the edmx file). When you do this, you effectively get rid of the code generation for the EF instance that you have loaded in your project. There are some excellent examples (and walkthroughs available) from the ADO.NET Team blog:

POCO in Entity Framework : Part 1 – The Experience (excellent walkthrough on removing the CustomTool)

POCO in Entity Framework : Part 2 – Complex Types, Deferred Loading and Explicit Loading

POCO in Entity Framework : Part 3 – Change Tracking with POCO

So now what? Great! So now I can use my POCO to update the Data Model. But I still hadn’t found out how to do true unit testing with DI and mocking? I was quite flummoxed until I realized (with a helpful pointer from a friend at Microsoft – thanks Jason) that the answer was staring me in the face:

“Can’t you create a mock class that derives from IObjectSet instance yourself, or is there a problem doing that?”

Well yes, I did have a problem with that – it meant that I would have to write more code. I was naively hoping to have something like:


List<Customer> cusList = TestHelper.CreateCustomerList();
IObjectSet<Customer> context = cusList.AsObjectSet();

So I was being lazy… I guess that, with each version of .NET, I had become more and more accustomed to so much being done for me that stumbling across something as “simple” as creating an AsObjectSet() function that was not available was bit of a shock. More so when you look at what IS available on a List<entity> method / property list.

At first I contented myself with just doing what was obvious – create a mock class that inherited from IObjectSet<Customer>, before I realized (with another push from Jason) that I could make it more generic and have a MockObjectSet<T> class:


    internal class MockObjectSet<T> : IObjectSet<T>
        where T : class
    {
        public MockObjectSet(List<T> entityList)
        {
            if (entityList == null)
            {
                throw new ArgumentNullException("entityList");
            }
            else
            {
                _repository = entityList.ToList();
            }
        }

        IList<T> _repository;

        #region IObjectSet<T> Members

        public void AddObject(T entity)
        {
            _repository.Add(entity);
        }

        public void Attach(T entity)
        {
            this.AddObject(entity);
        }

        public void DeleteObject(T entity)
        {
            _repository.Remove(entity);
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return _repository.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _repository.GetEnumerator();
        }

        #endregion

        #region IQueryable Members

        public Type ElementType
        {
            get { return typeof(T); }
        }

        public System.Linq.Expressions.Expression Expression
        {
            get { return _repository.AsQueryable<T>().Expression; }
        }

        public IQueryProvider Provider
        {
            get { return _repository.AsQueryable<T>().Provider; }
        }

        #endregion
    }

It is important to note here that the TestHelper.CreateCustomerList() function has several overrides and returns a List<Customer> filled with dummy data.

After playing around a bit I realized that I could actually just create an Extension Method that would create an instance of the mock CustomerSet and therefore I could call it from within my Unit Test code. The Extension Method looks like this:


    public static class ObjectSetExtension
    {
        public static IObjectSet<T> AsObjectSet<T>(this List<T> entities) where T : class
        {
            return new MockObjectSet<T>(entities);
        }
    }

Now if we revisit the unit test code, we get the following:


        [TestMethod]
        public void TestLoadValidCustomerContactNameWithSurname()
        {
            // Arrange
            // Create the stub instance
            INorthwindContext context = MockRepository.GenerateStub<INorthwindContext>();
            // Create the dummy data
            const string customerId = "555";
            const string contactName = "James Person";
            IObjectSet<Customer> customers = TestHelper.CreateCustomerList(contactName, customerId).AsObjectSet();

            // declare the dummy instance we are going to use
            Customer loadedCustomer;

            // Explicitly state how the stubs should behave
            context.Stub(stub => stub.Customers).Return(customers);

            // Create a real instance of the CustomerManager that we want to put under test
            Managers.CustomerManager manager = new Managers.CustomerManager(context);

            // Act
            manager.Load(customerId, out loadedCustomer);

            // Assert
            context.AssertWasCalled(stub => { var temp = stub.Customers; });
            // Check the expected nature of the dummy intance
            Assert.IsNotNull(loadedCustomer);
            Assert.IsNotNull(loadedCustomer.ContactName);
            Assert.IsTrue(loadedCustomer.ContactName == "James PERSON");
        }

If we compare the two managers again (the manager that I had created in a previous blog posting depended on EF 1.0), we will see that the EF 2.0 instance actually contains lambda expressions to do the queries.

EF 1.0:


    /// <summary>
    /// Loads a customer instance with the relevant information from the database.
    /// </summary>
    /// <param name="i_customerId">The customerId of the customer data to be retrieved.</param>
    /// <param name="o_customer">The customer instance to be created.</param>
    public void Load(string i_customerId, out Customer o_customer)
    {
        if (string.IsNullOrEmpty(i_customerId))
        {
            throw new ArgumentException("Parameter cannot be null.", "i_customerId");
        }
        int numericVal;
        if (!int.TryParse(i_customerId, out numericVal))
        {
            throw new ArgumentException("Parameter cannot be non-numeric.", "i_customerId");
        }
        if (numericVal < 0 || numericVal > 9999)
        {
            throw new ArgumentOutOfRangeException("i_customerId");
        }

        m_customerRepository.Load(i_customerId, out o_customer);

        if (o_customer != null)
        {
            if (!string.IsNullOrEmpty(o_customer.ContactName) && o_customer.ContactName.Contains(" "))
            {
                o_customer.ContactName = o_customer.ContactName.Trim(' ');
                string[] names = o_customer.ContactName.Split(' ');
                if (names.Length > 1)
                {
                    names[names.Length - 1] = names[names.Length - 1].ToUpper();
                }
                o_customer.ContactName = string.Join(" ", names);
            }
        }
    }

EF 2.0:


    /// <summary>
    /// Loads a customer instance with the relevant information from the database.
    /// </summary>
    /// <param name="i_customerId">The customerId of the customer data to be retrieved.</param>
    /// <param name="o_customer">The customer instance to be created.</param>
    public void Load(string i_customerId, out Customer o_customer)
    {
        if (string.IsNullOrEmpty(i_customerId))
        {
            throw new ArgumentException("Parameter cannot be null.", "i_customerId");
        }
        int numericVal;
        if (!int.TryParse(i_customerId, out numericVal))
        {
            throw new ArgumentException("Parameter cannot be non-numeric.", "i_customerId");
        }
        if (numericVal < 0 || numericVal > 9999)
        {
            throw new ArgumentOutOfRangeException("i_customerId");
        }

        var customers = (from cus in _context.Customers
                         where cus.CustomerID == i_customerId
                         select cus);
        if (customers.Count() == 1)
        {
            o_customer = customers.Single<Customer>();
        }
        else
        {
            o_customer = null;
        }

        if (o_customer != null)
        {
            if (!string.IsNullOrEmpty(o_customer.ContactName) && o_customer.ContactName.Contains(" "))
            {
                o_customer.ContactName = o_customer.ContactName.Trim(' ');
                string[] names = o_customer.ContactName.Split(' ');
                if (names.Length > 1)
                {
                    names[names.Length - 1] = names[names.Length - 1].ToUpper();
                }
                o_customer.ContactName = string.Join(" ", names);
            }
        }
    }

In the second code, snippet, because I am calling straight to an instance of IObjectSet<Customer>, it could either be my mocked one or the actual Entity Framework instance, which looks like this (thanks to POCO binding):


    public class NorthwindContext : ObjectContext, INorthwindContext
    {

        public NorthwindContext()
            : base("name=NorthwindEntities", "NorthwindEntities")
        {
        }

        private ObjectSet<Order> _orders;
        private ObjectSet<Employee> _employees;
        private ObjectSet<Customer> _customers;

        #region INorthwindContext Members

        IObjectSet<Employee> INorthwindContext.Employees
        {
            get { return _employees ?? (_employees = CreateObjectSet<Employee>()); }
        }

        IObjectSet<Customer> INorthwindContext.Customers
        {
            get { return _customers ?? (_customers = CreateObjectSet<Customer>()); }
        }

        IObjectSet<Order> INorthwindContext.Orders
        {
            get { return _orders ?? (_orders = CreateObjectSet<Order>()); }
        }

        #endregion
    }

This means that when I run my code coverage for the EF 2.0 version, I will be hitting the true boundary between the entity and the model, thanks to a combination of POCO and the separation of concerns brought about by IObjectSet.

Ok, so there’s no kitchen sink – what would you do with it if there was?

  • Share/Bookmark

Recently a colleague pointed out that my unit test were not following the AAA (Arrange, Act and Assert) structure that they had become accustomed to seeing and as they had understood Unit Tests should be. Although I had argued the point that there were many ways to setup and execute the test I can understand where the confusion can come in when mocking with Rhino Mocks.

I’m an advocate of using what comes natural to you as long as the outcomes are consistent; however I accept that there needs to be some uniformity when working in a team for the simple case of a collective understanding – as long as it is not at the behest of an individual’s ego.

The primary issue boiled down to the difference between the way that the test was prepared in my original blog posting:


[TestMethod]
public void TestInsertValidCustomerContactNameWithSurname()
{
    // Create the mock instance
    ICustomerRepository customerRepository = m_mockRepository.DynamicMock<ICustomerRepository>();

    Customer newEntry = TestHelper.CreateCustomer("James Person");

    // Now we set our expectations - when a null is passed the underlying method should throw an exception
    Expect.Call(() => customerRepository.Insert(newEntry)).Constraints(Property.Value("ContactName", "James PERSON"));

    // Replay our expectations
    m_mockRepository.ReplayAll();

    // Create a real instance of the CustomerManager that we want to put under test
    Managers.CustomerManager manager = new Managers.CustomerManager(customerRepository);

    manager.Insert(newEntry);
}

And setting it up in a true AAA sense:


[TestMethod]
public void TestInsertValidCustomerContactNameWithSurname()
{
    // Arrange
    // Create the stub instance
    ICustomerRepository customerRepository = MockRepository.GenerateStub<ICustomerRepository>();

    // Create the dummy instance to be used as a parameter
    Customer newEntry = TestHelper.CreateCustomer("James Person");

    // Create a real instance of the CustomerManager that we want to put under test
    Managers.CustomerManager manager = new Managers.CustomerManager(customerRepository);

    // Act
    manager.Insert(newEntry);

    // Assert - here we also check the property value ContactName to make sure that it is as expected.
    customerRepository.AssertWasCalled(stub => stub.Insert(newEntry), stub => stub.Constraints(Property.Value("ContactName", "James PERSON")));
}

Notice how the second example shows very distinct Arrange, Act and Assert structure.

Both methodologies result in the same outcome and at the end of the day I would say that you should use what works for you – as long as other people can understand the logic (“What if you should get hit by a bus?”).

  • Share/Bookmark

One way to use the Entity Framework on your legacy code

Following on the DI blog posts that I have been authoring lately, I wanted to discuss one possible method of stripping out the underlying data access layer (DAL) and introduce the Entity Framework from Microsoft. My intention is to write about how you would be able to use DI when introducing the new DAL.

Starting with what we already have.

In many cases there will be some form of data access layer manager class that will act as the proxy to client code when performing the request to the database and translating the returned query into “code entities”. This would be the ideal place to start the transition (obviously). Capitalizing on our earlier example of the NorthWind database, let’s assume that we have the following classes:

Base Classes

As you will see, we have two DAL related classes; EmployeeRepository and CustomerRepository. Both of these expose typical CRUD methods that we will refactor into interfaces. Once the principal methods have been extracted into interfaces then we can create the new client facing entities that will provide access to the DAL via the appropriate methods and functions:

Manager Classes

As you can see from the class diagram, each of the manager classes has two constructors; the first (parameter-less) constructor is the default and contains no code except for calling into the second constructor; the second (single parameter) constructor contains the assignation of a member variable to the associated repository interface:

  1. /// <summary>
  2.     /// Parameterless (Default) constructor
  3.     /// </summary>
  4.     public EmployeeDataManager() : this(null)
  5.     {
  6.     }
  7.  
  8.     /// <summary>
  9.     /// Parameterized constructor
  10.     /// </summary>
  11.     /// <param name="i_repository">The IEmployeeRepository instance to
  12.     /// be used.</param>
  13.     internal EmployeeDataManager(IEmployeeRepository i_repository)
  14.     {
  15.         m_repository = i_repository;
  16.     }

One important note to make and something that may or may not seem strange is the definition of the second constructor as internal as opposed to public. If you remember from a previous post, by defining the second constructor as internal and then exposing it only to the unit tests (via the assembly attribute InternalsVisibleTo) – this way when coding the clients of the manager, we will not place the onus of instantiating a repository on the client (unless you wish to do so).

Later on, once we have the DAL utilizing the Entity Framework in place, we can extend the assignation of the repository interface to also include the instantiation of our default DAL.

Incorporating the Entity Framework

The first stage is to create a suitable assembly where the Entity Framework related classes will reside and be referenced from. Therefore we could create an entity access assembly:

Pre Entity Framework

Now we can go through the steps of incorporating the Entity Framework:

  1. In the EntityAccess project, add a new “ADO.Net Entity Data Model” item:

    Stage 1
  2. In this example we want to create the model contents from the actual DB instance:

    Stage 2
  3. The next window of the Entity Data Model Wizard specifies the Data Connection that will be used. Depending on your requirements this will vary from situation to situation:

    Stage 3
  4. In this case we will create a new one for our purposes; click the “New Connection…” button to display the Connection Properties dialog (Fill-out the fields as appropriate to your purposes) and select the Server and the Database instance:

    Stage 4
  5. Once the Connection Properties have been configured, the appropriate information will appear:

    Stage 5
  6. Clicking the “Next >” button will take us to the “Choose Your Database Objects” window, where we can select the tables that we are interested in (seeing as it is such a small schema, we will select all tables in this example):
    Stage 6
  7. Once we have clicked on the “Finish” button the associated edmx file (XML file that defines an Entity Data Model) and it’s associated code behind have been generated:
    Stage 7 - a


    Test Stubs Dialog
  8. For consistency reasons (and personal preference) I normally change the default entity naming convention as follows, renaming the pluralized to singular:
    Stage 8 - a

    becomes

    Stage 8 - b


    Stage 8 - c

Now that we have added the Entity Framework into the solution we can set about incorporating the use of the appropriate repository interfaces (ICustomerRepository and IEmplyeeRepository).

Delving into the code generated when the Entity Data Model was generated (NorthWind.Designer.cs) we find that the class is defined as partial:

  1. /// <summary>
  2.     /// There are no comments for NorthwindSQLEntities in the schema.
  3.     /// </summary>
  4.     public partial class NorthwindSQLEntities : global::System.Data.Objects.ObjectContext
  5.     {
  6.      …
  7.     }

This means that we could use the same class to implement the two interfaces we already have defined:

Stage 9 - a

Unfortunately when we attempt this method of implementing the repository interfaces we encounter the following error on the first attempt at accessing the database via the entity data model:

Stage 9 - b

With this issue raised it is easier to sub-class the Entity Data Model generated class and implement the interfaces in the child class:

Stage 9 - c

Further digging around in the auto-generated Entity Data Model reveals the different overloads for the constructor:

  1. /// <summary>
  2.     /// Initializes a new NorthwindSQLEntities object using the
  3.     /// connection string found in the ‘NorthwindSQLEntities’ section
  4.     /// of the application configuration file.
  5.     /// </summary>
  6.     public NorthwindSQLEntities() :
  7.             base("name=NorthwindSQLEntities", "NorthwindSQLEntities")
  8.     {
  9.         this.OnContextCreated();
  10.     }
  11.     /// <summary>
  12.     /// Initialize a new NorthwindSQLEntities object.
  13.     /// </summary>
  14.     public NorthwindSQLEntities(string connectionString) :
  15.             base(connectionString, "NorthwindSQLEntities")
  16.     {
  17.         this.OnContextCreated();
  18.     }
  19.     /// <summary>
  20.     /// Initialize a new NorthwindSQLEntities object.
  21.     /// </summary>
  22.     public NorthwindSQLEntities(global::System.Data.EntityClient.EntityConnection connection) :
  23.             base(connection, "NorthwindSQLEntities")
  24.     {
  25.         this.OnContextCreated();
  26.     }

With this in mind we can create a constructor that takes 4 parameters that can be used to generate the appropriate connection string:

  1. /// <summary>
  2.     /// Creates a new NorthwindEntityContext.
  3.     /// This class will be the handler for all CRUD operations
  4.     /// perfomed by the EntityAccess layer.
  5.     /// A connection is created from this constructor.
  6.     /// </summary>
  7.     public NorthwindSQLEntities(
  8.         string i_serverName,
  9.         string i_catalogName,
  10.         string i_user,
  11.         string i_pswd)
  12.             : base(BuildConnectionString(
  13.                 i_serverName,
  14.                 i_catalogName,
  15.                 i_user,
  16.                 i_pswd))
  17.         {
  18.            
  19.         }

The BuildConnectionString function would construct the appropriate connection string as follows:

  1. private static string BuildConnectionString(string i_serverName, string i_catalogName, string i_user, string i_pswd)
  2.     {
  3.         string serverName = Environment.MachineName;
  4.         if (!string.IsNullOrEmpty(i_serverName))
  5.             serverName = i_serverName;
  6.         string catalogName = "NorthwindSQL";
  7.         if (!string.IsNullOrEmpty(i_catalogName))
  8.             catalogName = i_catalogName;
  9.  
  10.         string AdoConnectionString = string.Empty;
  11.         if (!string.IsNullOrEmpty(i_user))
  12.         {
  13.             AdoConnectionString = string.Format(
  14.             "Data Source={0};Initial Catalog={1};Integrated Security=False;MultipleActiveResultSets=True;Uid={2};Pwd={3}",
  15.             serverName, catalogName, i_user, i_pswd);
  16.         }
  17.         else
  18.         {
  19.             AdoConnectionString = string.Format(
  20.                 "Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True",
  21.                 serverName, catalogName);
  22.         }
  23.         return string.Format(
  24.             "metadata=res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl;provider=System.Data.SqlClient;provider connection string=\";{1}\";",
  25.             EntityModelName, AdoConnectionString);
  26.     }

The Entity Framework provides the appropriate mapping between the Storage Model (Database) and the Conceptual Model (entities), however, in order to be able to implement Dependency Injection we need to maintain (or create) a disconnected set of implementation classes that are agnostic to the underlying repository (be it Entity Framework or any other data access model, such as NHibernate, etc.).

Therefore the CRUD methods that we have exposed through our original interfaces, in the Entity Framework domain, would provide a certain amount of “translation” from the disconnected entity type to the “connected” (EDM) type:

  1. using BaseCustomer = DiDemo.BaseData.Entities.Customer;
  2. using DiDemo.BaseData.Interfaces;
  3.  
  4. namespace DiDemo.EntityAccess
  5. {
  6.     public partial class NorthwindSQLEntities :
  7.         ICustomerRepository, IEmployeeRepository
  8.     {
  9.     /// <summary>
  10.     /// Insert a new Customer instance into the Database.
  11.     /// </summary>
  12.     /// <param name="i_customer">
  13.     /// The Customer instance to be inserted.
  14.     /// </param>
  15.     public void Insert(BaseCustomer i_customer)
  16.     {
  17.         Customer customer = new Customer()
  18.             {
  19.                 Address = i_customer.Address,
  20.                 City = i_customer.City,
  21.                 CompanyName = i_customer.CompanyName,
  22.                 ContactName = i_customer.ContactName,
  23.                 ContactTitle = i_customer.ContactTitle,
  24.                 Country = i_customer.Country,
  25.                 CustomerID = i_customer.CustomerID,
  26.                 Fax = i_customer.Fax,
  27.                 Orders = new EntityCollection<Order>(),
  28.                 Phone = i_customer.Phone,
  29.                 PostalCode = i_customer.PostalCode,
  30.                 Region = i_customer.Region
  31.             };
  32.         this.AddToCustomerSet(customer);
  33.         this.SaveChanges();
  34.     }

Unit Testing

Now that we have the underlying Data Access Layer sorted out, time to move on to the reason for this blog posting – Unit Testing.

Obviously the very basic CRUD methods and functions do not provide any head banging issues when it comes to unit testing, however, suppose there a function returned an IQueryable instance? It could possibly be conceived that such a method existed as follows:

  1. /// <summary>
  2.     /// Returns a List of Customer instances
  3.     /// </summary>
  4.     /// <returns>List containing all Customer instances.</returns>
  5.     public List<BaseCustomer> LoadAllCustomers()
  6.     {
  7.         return Query_LoadAllCustomers().ToList();
  8.     }
  9.  
  10.     /// <summary>
  11.     /// Returns an IQueryable for the Customers entity set.
  12.     /// </summary>
  13.     /// <returns>IQueryable of the customer enitity set.</returns>
  14.     public IQueryable<BaseCustomer> Query_LoadAllCustomers()
  15.     {
  16.         return (from query in CustomerSet
  17.                 select new BaseCustomer()
  18.                 {
  19.                     Address = query.Address,
  20.                     City = query.City,
  21.                     CompanyName = query.CompanyName,
  22.                     ContactName = query.ContactName,
  23.                     ContactTitle = query.ContactTitle,
  24.                     Country = query.Country,
  25.                     CustomerID = query.CustomerID,
  26.                     Fax = query.Fax,
  27.                     Phone = query.Phone,
  28.                     PostalCode = query.PostalCode,
  29.                     Region = query.Region
  30.                 });
  31.     }

Surprisingly enough it is not that difficult to create the unit test for such a method in the Manager class. Seeing as we have successfully done the dependency injection prior to creating the entity framework related classes, all we are actually doing is writing the test for the manager class by mocking the repository interface:

  1. [TestMethod]
  2.     public void TestIQueryableOnEntity()
  3.     {
  4.         // Create the mock instance
  5.         IEmployeeRepository eConnect = m_mockRepository.DynamicMock<IEmployeeRepository>();
  6.  
  7.         // Here we create an actual instance
  8.         IQueryable<Employee> mockedCollection;
  9.  
  10.         // Setup a dummy list that will be filtered, queried, etc
  11.         List<Employee> employeeList = CreateEmployeList();
  12.         mockedCollection = employeeList.AsQueryable();
  13.  
  14.         // Now we set our expectations
  15.         Expect.Call(eConnect.Query_LoadAllEmployees()).Return(mockedCollection);
  16.  
  17.         // Replay our expectations
  18.         m_mockRepository.ReplayAll();
  19.  
  20.         // Create a real instance of the EmloyeeConnector that we want to put under test
  21.         EmployeeManager manager = new EmployeeManager(eConnect);
  22.  
  23.         List<Employee> employees = manager.Query_LoadAllEmployees().ToList();
  24.  
  25.         Assert.IsNotNull(employees);
  26.  
  27.         Assert.IsTrue(employees.Count == employeeList.Count);
  28.     }

The secret is in the AsQueryable method on the List instance. By doing this we can simulate the behavior of the IQueryable function on our Entity Data Model.

  • Share/Bookmark

What’s in a name?

Google “Pex” and what do you expect to find? According to Wikipedia:

“Cross-linked polyethylene, commonly abbreviated PEX or XLPE, is a form of polyethylene with cross-links.”

That’s not really the definition I was hoping for, so if we qualify the search with “Microsoft” then we have a better chance of encountering a more meaningful software related defintion:

“Pex (Program EXploration) produces a traditional unit test suite with high code coverage. A parameterized unit test is simply a method that takes parameters, calls the code under test, and states assertions. Given a parameterized unit test written in a .NET language, Pex automatically produces a small unit test suite with high code and assertion coverage. To do so, Pex performs a systematic white box program analysis.”

Well, now we have got that cleared up, let’s have a look at what we can do with it.

For a start, the most powerful aspect of Pex is the white box analysis that it performs on an existing unit of code. This analysis will create the appropriate boundary test (as best as it can). The generation of test code is not extensive; it covers the simple cases and where complex data type are used, the developer will most likely still have to fall back on utilizing test methods such as mocking.

Part of the secret of getting Pex to work your code properly is to provide some form of Parameterized Unit Test to get it started. Another important factor is to use the Stubs Lightweight Framework (also a research project from Microsoft) to create appropriate stubs for the Parameterized Unit Tests. Both methods can be generated quite easily; however a few tweaks are required to get it just right.

In order to illustrate the work involved we shall revisit the previous example of one of the Managers (either EmployeeManager or CustomerManager). The following walkthroughs assume that you have Pex installed and that you have either Visual Studio Test edition or Team edition installed.

How it is done using defaults…

In the first stage we need to instruct the assembly being tested that it will give the new unit test assembly access to the internal methods and properties (see my previous post):

  1. [assembly: InternalsVisibleTo("DiDemo.Managers.Tests")]

The second stage is to create the Unit Test project that will “house” the Pex generated unit Tests. The easiest way to do this is to use the Pex auto-generation functionality from the Code Context menu:

  1. Right click the class name that will have the unit tests generated (in our case CustomerManager) and select the Pex->Create Parameterized Unit Test Stubs:

    Generate Test Stubs
  2. Configure the Unit Test project that you want to target the auto-generated code to (in this case we will select from the drop down combo box) and click Ok when you are ready:

    Test Stubs Dialog
  3. Specify (if you wish) the location of the new Test project and click OK:

    Test Stubs Dialog
  4. The result will be the generation of the new Unit Test project complete with auto-generated Stubs and Parameterized Unit Test Stubs:

    Generated Unit Tests

Having a quick look at what is generated, we see that the stubx file is the configuration xml for the Microsoft Stubs engine that indicates which assembly we are generating the stubs from – thereby creating the stubs designer file:

Generated Stubs

As we will see later on, the stubs that are generated in this “default” manner might not be exactly what we want, although the concept is going to come in handy.

Next is the auto generated parameterized unit test stubs. These methods are intended to be used by Pex to generate the unit test methods when the analysis is run. In their current format they do little but to serve as place holders:

Generated Test Methods

At this stage, as an educational exercise, we can run the Pex Explorations to see what gets thrown up:

Run Pex Explorations

Looking through the results there is one glaring issue and that is the fact that the way we have created the architecture of the managers (using Dependency Injection and having default behavior); we have inadvertently generated integration tests (the clue is in the fact that we are seeing SqlExceptions – this means that we have also explored the underlying data access layer, which we had tried so hard to inject the dependency for:

First Results Pex Explorations

The thought through way…

In my mind we don’t want to generate stubs of the classes that we are actually testing, we want to generate stubs of the interfaces that we are injecting into the classes we are testing – a subtle difference, but nevertheless one that is important.

Therefore, strip out the constructor tests (parameter-less and single DI parametered constructor):

Delete Constructor Tests

After those test methods are removed, delete the Manager stubs (we aren’t interested in stubbing out the class we are testing):

Delete Stubs

Finally we want to generate the stubs for the Interfaces, so we need to create a stub of the BaseData assembly:

Generate Data Stubs

Now that the stubs are generated, we can go about changing the parameterized test methods to fit our own purposes. Taking the first method Delete, we make the changes to the method in such a way that the method itself creates the class instance we want to test and the parameter provides us with the boundary conditions. This means that our code changes from this:

  1. /// <summary>Test stub for Delete(Customer)</summary>
  2. [PexMethod]
  3. public void Delete([PexAssumeUnderTest]CustomerManager target, Customer i_customer)
  4. {
  5.     // TODO: add assertions to method
  6.     // CustomerManagerTest.Delete(CustomerManager, Customer)
  7.     target.Delete(i_customer);
  8. }

to this:

  1. /// <summary>Test stub for Delete(Customer)</summary>
  2. [PexMethod]
  3. public void Delete(Customer i_customer)
  4. {
  5.     // Create a stub instance of the ICustomerRepository
  6.     var stub = new SICustomerRepository();
  7.     stub.Delete = (a) => { };
  8.     ICustomerRepository repository = stub;
  9.     // Create a real instance of the CustomerManager that we want
  10.     // to put under test
  11.     CustomerManager manager = new CustomerManager(repository);
  12.     manager.Delete(i_customer);
  13. }

Once all of the parameterized unit tests are prepared we can run the Pex Explorations again:

Run Pex Explorations

The result this time is quite different to what we had previously with the outcome being more conclusive than the first. Upon inspection we actually find some test scenarios that we didn’t cover in our code:

Second Results Pex Explorations

The general idea is to continue to adjust the code of your class to make sure that all of the test cases are handled correctly until you can achieve, as far as possible, test case completeness:

Completed Test Scenario

The next stage is to run the tests to see what the code coverage is:

Completed Code Coverage

You will see that we didn’t get 100% code coverage because of the method “<LoadAllCustomers>b__0(class DiDemo.BaseData.Entities.Customer,class DiDemo.BaseData.Entities.Customer)”;which is the following area of code:

Last five percent

Conclusion

What is on offer by using Pex is quite astounding, but by no means bullet proof. There are still cases that you will want to write your own test method to try to reach the nirvana of 100% code coverage; however what Pex does do is to take care of the majority of cases that you would grudgingly have to knock out by hand.

The other niggling issue in my mind is the fact that the stubs have to be re-generated anytime there is a change made to a dependant class, which might seem like nit-picking, it is still something that could be overlooked during development – thereby rendering the unit test as unreliable.

On the whole I am quite impressed by what is on offer and I could foresee it’s use when the wider .NET development community starts to walk around and kick the tires a bit more, maybe even take it out for a few laps.

  • Share/Bookmark

Whilst doing further research into the topic of Dependency Injection and Rhino Mocks I came across a blog entry that gave me a certain amount of food for thought. The article spoke about using the friend assembly directive in C# and how you could effectively hide the injected constructor from all but your unit test assembly. From MSDN:

The friend assemblies feature allows you to access internal members; private types and private members will remain inaccessible.
To give an assembly (assembly B) access to another assembly’s (assembly A’s) internal types and members, use the InternalsVisibleToAttribute attribute in assembly A.

Revisiting the code from the examples used in the previous articles we would need to add the attribute to the class declaration so that the Unit Test assembly “BlogSamples.UnitTest” would be allowed access to the internal constructor:

  1. /// <summary>
  2. /// Single parameter constructor using dependency injection pattern.
  3. /// </summary>
  4. /// <param name="i_employeeRepository">An object instance that implements the IEmployeeRepository interface.</param>
  5. internal EmployeeManager(IEmployeeRepository i_employeeRepository)
  6. {
  7.     Initialize(i_employeeRepository);
  8. }

We would also have to add the InternalsVisibleTo attribute declaration to the AssemblyInfo.cs file:

  1. [assembly: InternalsVisibleTo("BlogSamples.UnitTest")]

This would have the effect of allowing the “BlogSamples.UnitTest” assembly access to all internal members of the “BlogSamples.DbConnector” assembly. This can be easily seen when utilizing the intellisense whist within the code of a “BlogSamples.UnitTest” class:

Visible Internal Constructor

Whilst any other assembly accessing the members of “BlogSamples.DbConnector” would only see the parameter-less constructor:

Default Constructor only

This would mean that the existing client’s remain un-changed, whilst future development will not be able to use the Constructor Injection design of the EmployeeConnector class as it would be hidden via the internal directive.

  • Share/Bookmark

Although I have no concrete numbers, there are numerous software development projects that are well intentioned when it comes to unit testing; however their actual implementation of unit testing falls woefully short of what is considered proper / correct unit testing.

Many times it is because of time constraints and is more of a case of ticking a task as done, when in actual fact what has happened is that legacy code has made it prohibitively time consuming to do correct unit testing. For the most part, teams are doing light integration testing by using existing components to test the class that they are supposedly writing the unit test for. Other times it may be the lack of understanding of what Unit Testing really implies. Quite often the developer will write the unit test with the intention of only testing the class that they are interested in, but have not thought about the way in which the class needs to be isolated in order to perform the testing. The most common culprit causing teams to fall short of correct unit testing are classes that are dependant on database connectivity. I have seen so many cases where the design of the class has been left untouched with the belief that because the tests are concentrated on the class, then it is considered unit testing. In most of the cases I have seen, Unit Testing has been an activity that is planned for towards the end of the implementation and therefore is the activity that receives the least amount of attention (if at all).

Well sadly, despite those best efforts and intentions, it is not proper unit testing. At the end of the day, it turns out that the class in question is not the only unit being tested; quite often it is also the data access layer that comes under test as well.

I think we have all fallen in to this trap, I myself have been guilty of it and I doubt very much there isn’t a single individual that hasn’t fallen in to this “lie” at one time or another. A unit test needs to be just that; testing one unit of code. The code paths that you are testing must reach down to the boundaries of the next class or resource, without actually traversing into that class or resource. I am sure that those of us who have been down this route will being nodding their heads (guiltily) and asking themselves the same question that I did “How can you test a class without data?”

This is true; you do need data for your tests. But it needs to be controlled data, data that the test generates and you know exactly what the outcome of that test is going to be. Thinking about it now, I was very fortunate in my early career to have the opportunity to work for a company that produces both software AND hardware upon which the software was to operate. In the same building we had software and hardware engineers, it was the first time I had the opportunity to see for myself a real test bed and test harnesses – and I’m not talking about the software type either, I am talking about true, honest to goodness test harnesses. The machine testing the PCB (printed circuit board) actually had probes that poked down onto the board and “injected” signals onto the circuits at key points to test the appropriate reaction of the circuits under test. Although this analogy does not truly reflect unit testing as there were points in the circuit that would have meant actually testing the signal across one or two other components, it serves as a useful point of reflection.

The data needs to be injected into the class and should only affect the class under test. Otherwise, how can you tell that the failure of the test is not actually related to an underlying component or some issue with database connectivity? For those of us working in the object driven development environment, we have had the answer staring us in the face all the time. Dependency Injection!

It might sound like a buzzword, but DI has been around for some time. It is based on the use of Interfaces. Yup, that’s what I said – Interfaces. Although the excessive use of interfaces can make it very difficult to know exactly what is the real object that is being passed in to a class, the truth is, as the class under test – do you really care? The answer is No not really. Remind yourself what Interfaces are intended for. They are, in effect, contracts between two (or more) instances. They are an agreement between the parties that x will provide y with data or an action upon data.

If you think of your OO design through the use of Interfaces rather than concrete instantiation of types from directly within your component classes, then you are on your way to using the DI pattern.

Update (April-07-2009): I had previously quoted an article from Wikipedia, which in hindsite was not how I wanted to present DI. Although the article provides a definition of DI, it does so by basing it on Inversion of Control (to achieve Inversion of Control you need to use Dependency Injection). As Martin Fowler states in his article on Inversion of Control, IoC is the container or framework that is made possible by DI. I will cover IoC and it’s practical uses ata later date, but for now I want to concentrate on DI.

By defining the interaction between your classes in terms of interfaces rather than through the use of strongly typed classes then you are immediately starting to de-couple those classes. The idea is simple. By removing the dependency on a concrete Class and shifting it to an Interface, you are in effect, facilitating the implementation of true unit tests.

Thinking about the unit tests you have written previously, I am sure that the majority of us will agree that it has always been necessary to include the use of another class, another instance to actually be able to perform tests on the unit in question. Go on, you’re amongst friends, no-one will laugh. Whisper it out loud if you want to. We could ask ourselves the following questions to see if what we have written are real honest to goodness unit tests or just very light weight integration tests:

  • Does the unit require an instance of any other complex class in order to be able to test it? (By complex class I am referring to a class that will cause the code path from your unit test to perform an type CRUD operation on the data received from/petitioned by your component)
  • If it is included in a build system, does the build system have to have any special configuration / installation to be able to run the unit test?
  • Does there need to be connectivity to a database/network prepared for the unit test to run?


I am sure as time goes by I will add more to this list, but for now you get the general idea. If you have answered yes to any of those questions, then it is NOT a unit test. What we have here then, is integration testing. The code path has not been contained within the unit and therefore we have inadvertently involved one or more classes. The results of the test are then questionable as they do not reflect the actions performed solely within the unit under test.

In the next article I would like to take an example of a legacy laden class and show how we can apply some DI principles to it so that we prepare it for true unit testing.

  • Share/Bookmark