Archive for the ‘Agile’ Category
Monday, January 19th, 2009



This post is part of a series on Test Driven Development – Using Dependency Injection

We introduced some terms previously to help describe some of the things we need to do in order to make our code more testable and started the refactoring of our solution in Step 1. In this step we are going to complete the refactor and implement Dependency Injection in the RandomMessage class.

There are a couple of ways to pass in the object we want to work with; we will use the Constructor as this is probably the most common and easiest to use.  We have a few things that we need to do to complete the refactoring

1. Updated the tests to pass a RandomNumber object into the constructor of GetMessage

2. Update the GetMessage class to support the passing in of the RandomNumber object

3. Create a fake of the RandomNumber class that implements the IRandomNumber interface so that we can test our code

4. Confirm we have fixed our test and all tests pass.

Because we previously refactored out test code to have a setup method we only have to make one change to use the planned constructor.

[SetUp]
public void SetUp()
{
      message = new ObjectModel.RandomMessage(new RandomNumber());
}

We then update our production code to support this new Constructor, it should look like this with the constructor highlighted.  The GetRandomMessage method has also been updated to use the _randomNumber variable.

    public class RandomMessage
    {
        IRandomNumber _randomNumber;
        public RandomMessage(IRandomNumber randomNumber)
        {
            _randomNumber = randomNumber;
        }
        public string GetRandomMessage(string WebUrl)
        {
            string message = "";
            try
            {
                using (SPSite site = new SPSite(WebUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList messages = web.Lists["Messages"];
                        int rndMessage = _randomNumber.GetRandomNumber(messages.ItemCount);
                        message = messages.Items[rndMessage].Title;
                    }
                }
            }
            catch (UriFormatException)
            {
                message = "Error: Please provide a site url for the message list.";
            }
            catch (ArgumentException)
            {
                message = "Error: Messages list could not be found";
            }
            catch (FileNotFoundException)
            {
                message = "Error: Site could not be found";
            }
            return message;
        }
    }

To get the solution to compile you can update the WebPart class with the similar change to the one in the tests.

Running the tests you should see your previous tests continue to work, showing once more the value of the tests – we refactor with confidence, we know that when done our production code still does what the developer wanted it to do.. it continues to meet the specification.

Meets the specification,  what am I taking about!  Doesn’t the code pass the tests?

I’ll leave this question with you for now,  I will be talking about this a lot over the coming months..

Back to our Red bar which should look something like this

image

The observant among you will have noticed that the error has changed.  We are no longer getting a warning from Isolator about unsupported objects.  What we now have is a genuinely failing test.   There is a chance that your test may have passed, its a slim chance, as our test was asserting that we returned the 100th message from the list.  If your call to the GetRandomNumber returned 99 it will match.

Obviously this is not acceptable, and is the reason we introduced Dependency Injection.   Rather than faking the return from GetRandomNumber using Isolator (mocking) we are going to pass in our object that implements the same IRandomNumber interface.  If you remember back to when we Introduced some terminology we talked about loose coupling.   Our GetRandomMessage class is now loosely coupled to the RandomNumber class so we can create our own, based on a common Interface, and use this instead.

We create our own test class that allows us to define the values returned from the GetRandomNumber method.  You could put this in it’s own .cs file depending on how you like to structure your projects – personally I keep this in the same file as the tests that use it.

Continuing the principle of only coding what we need to our new fake class should look something like this

    public class FakeRandomNumber : ObjectModel.IRandomNumber
    {
        #region IRandomNumber Members
        public int GetRandomNumber(int max)
        {
            return 99;
        }
        #endregion
    }

We have hard coded the return value of 99 as this is what we want to fake – to return the last item in the list.  If we added more tests that need different values we may extend this with a simple property that allows us to set the return value within the test.

Update the test to use the FakeRandomNumber we just created, we will need to define our own instance of the RandomMessage class.

//Act
ObjectModel.RandomMessage message = new ObjectModel.RandomMessage(new FakeRandomNumber());
string rndMessage = message.GetRandomMessage("http://validsiteurl");

We have our green bar,  and everything passes.

image

In the final part of the series we will review the changes we have made and how TDD really does help you design better code.

TDD SharePoint – Using Dependency Injection Summary

Tuesday, January 13th, 2009



This post is part of a series on Test Driven Development – Using Dependency Injection

We introduced some terms previously to help describe some of the things we need to do in order to make our code more testable.  In this step we are going to start the refactor to  support Dependency Injection in the RandomMessage class.

The code that is causing us a problem when using Isolator for SharePoint is in the GetRandomMessage method.

        public string GetRandomMessage(string WebUrl)
        {
            string message = "";
            try
            {
                using (SPSite site = new SPSite(WebUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList messages = web.Lists["Messages"];
                        int rndMessage = GetRandomNumber(messages.ItemCount);
                        message = messages.Items[rndMessage].Title;
                    }
                }
            }
//*... snipped for brevity ...*//
        }
    }

What we need to do is use Dependency Inversion, that is we want to allow our calling code to decide what is used to get the random number.  To achieve this we need to create an interface for a class that we will allow us set define the way get our random number.

Visual Studio was really designed with agile developers in mind.   In order to achieve our refactoring we can do the following.

Highlight the method GetRandomNumber, right click on the code and choose Refactor.  Should see something similar to the image below

image

Choose the Extract Interface option which will provide you with a dialog to define the new interface name (IRandomNumber), the .cs file and also the methods that will be extracted.

image

Click OK and Visual Studio will do some of the work for us.  It does not however do everything so we will continue to fix up the code.

1. In the newly created IRandomNumber.cs files make the interface Public

As we are doing TDD we need to review our tests as the ones for the GetRandonNumber method will now be against a new object.  You may want to split out the tests into another class or .cs file but for now we will just refactor the tests that will change.

The tests should look something like below,  the code won’t at this point compile as we don’t yet have this class.

        [Test]
        public void GetRandomNumber_PostiveRange_ReturnValueInRange()
        {
            ObjectModel.RandomNumber randomNumber = new ObjectModel.RandomNumber();
            int messageId = randomNumber.GetRandomNumber(1);
            Assert.AreEqual(0,messageId);
        }
        [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void GetRandomNumber_Zero_ThrowException()
        {
            ObjectModel.RandomNumber randomNumber = new ObjectModel.RandomNumber();
            int messageId = randomNumber.GetRandomNumber(0);
        }
        [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void GetRandomNumber_Negative_ThrowException()
        {
            ObjectModel.RandomNumber randomNumber = new ObjectModel.RandomNumber();
            int messageId = randomNumber.GetRandomNumber(-1);
        }
        [Test]
        public void GetRandomNumber_MaxValue_ReturnValueInRange()
        {
            ObjectModel.RandomNumber randomNumber = new ObjectModel.RandomNumber();
            int messageId = randomNumber.GetRandomNumber(int.MaxValue);
            Assert.GreaterOrEqual(messageId, 0);
            Assert.LessOrEqual(messageId, int.MaxValue);
        }

Add a new class to the UnitTest.ObjectModel project called RandomNumber.  Make this class public and implement the interface IRandomNumber.  Next we need to remove the code from the RandomMessage class.   This involves removing the implementation of the IRandomNumber interface that Visual Studio added and also moving the GetRandomNumber code into our new class which should look similar to the code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnitTest.ObjectModel
{
    public class RandomNumber : IRandomNumber
    {
        public int GetRandomNumber(int max)
        {
            if (max < 1)
                throw new ArgumentOutOfRangeException(
                    "max", max, "Maximum value must be greater than or equal to 1");
            Random rnd = new Random();
            return rnd.Next(max);
        }
    }
}

At the moment our code will be in a state of flux,  it still won’t compile as the GetRandomMessage method needs to be updated,  we will add a new instance of the RandomNumber class and call this method on this.  We’re not doing the dependency injection just yet.

Once this change is implemented we should be able to compile the UnitTest.ObjectModel project.   To compile the tests we need to comment out the call we make to mock out this method (this was the method that caused us issues testing with Isolator for SharePoint).

In the method GetRandomMessage_MultipleMessages_ReturnMessage comment out the line

//Isolate.WhenCalled(() => message.GetRandomNumber(1000)).WillReturn(99);

Our solution should now compile and running the tests all except the one above should pass.

A quick recap shows that we have refactored our solution, extracting the RandonNumber method into it’s own class based on an interface and run all the tests to show the code works as expected.   This is one of the big values of doing TDD,  we can make some major refactors in our code and know when we’re complete because the tests pass.

In the next next step we are going to use the IRandomNumber interface to implement Dependency Injection.

Step 2. Implementing dependency injection into GetRandomMessage

Friday, January 9th, 2009



This post is part of a series on Test Driven Development – Using Dependency Injection

There are a number of terms that you are likely to hear very early on in your journey to Test Driven Development, these terms generally refer to specific patterns of coding that have developed to support particular problems.  You should be familiar with terms like Singleton and Factory which are used as a common vocabulary and help developers to communicate their design.

In order to complete the refactoring of our project we will introduce some new terms Loosely Coupled, Dependency Inversion, Dependency Injection and Inversion of Control to help us describe the changes and the reasoning behind them.

Loosely Coupled

Coupling refers the relationship between two modules. In a loosely coupled relationship the modules relate to each other through a common interface and are not concerned with the other modules internal implementation. Changes to one module does not require a change in the implementation of the other; one of the benefits of this loose coupling is that it makes testing easier, it allows us to replace the real modules with fake ones during testing. The fake modules will implement the same common interface allowing the module under test to work without any changes.

In order to be able to achieve this loose coupling we need to implement the Dependency Inversion principle.

Dependency Inversion

The Dependency Inversion principle is used to ensure your code is loosely coupled and therefore more testable. Often people will not mention this instead they will refer to Inversion of Control or Dependency Injection, described below, however when dealing with Unit Testing Dependency Inversion is really at the heart of what we are doing.

In simple terms dependency inversion means we flip things so that the calling code is the one that decides which implementation will be used, hence ‘inversion’.

In order to achieve this we use Dependency Injection.

Dependency Injection

This really is a grand term for what is a very simple concept a way of providing an object with the variables it needs.

Dependency Inversion = give the calling code control over which variables are used

Dependency Injection = a way to pass those variables into the object

An example here will probably help, the code sample shows how the Sample object (class) defines and instantiates the RandomNumber object (class)

public class Sample
{
    private RandonNumber _randomNumber;
    public Sample()
    {
        _randomNumber = new RandomNumber();
    }
    public string GetRandomMessage()
    {
        ...
        int messageId = _randomNumber.GetRandomNumber(itemCount);
        ...
    }
}

This example is tightly coupled as the Sample object explicitly creates the RandomNumber object that is then uses internally, the code below demonstrates how we have refactored this to use the Dependency Injection pattern and enable loose coupling.

public class Sample
{
    private RandonNumber _randomNumber;
    public Sample()
    {
        _randomNumber = new RandomNumber();
    }
    public Sample(RandomNumber randomNumber)
    {
        _randomNumber = randomNumber;
    }
    public string GetRandomMessage()
    {
        ...
        int messageId = _randomNumber.GetRandomNumber(itemCount);
        ...
    }
}

Passing an object into the constructor means we now have Dependency Injection as we can pass the RandomNumber object in; and Dependency Inversion as our calling code controls which type of object is used.

A surprisingly simple concept and implementation that tends to be over complicated. There are a few other ways of achieving the dependency injection through setters and interfaces  but these are just variation on the same concept.

If you now think about this for a moment and consider the common layers you have in your application this could be Data Access Layer (DAL), Business layer, UI and possibly others like payment services and authentication, this dependency inversion at each layer would mean that ultimately all of your dependent objects need to be defined at the highest level the UI which is not really a good idea. To address this we have the Inversion of Control pattern.

Inversion of Control

When Inversion of Control is discussed today it can be used in place of Dependency Injection or more likely as a reference to the frameworks that help to manage the dependencies of loosely coupled systems. These may also be called Inversion of Control Containers.

We are not going to look at Inversion of Control as part of this series, however it is useful to be aware of the terminology.

In the next part of our series we are going to look at implementing the changes in our project, starting with the abstracting of a common interface.

Step 1. Refactoring

Further Reading

The concepts above are not new and there is a lot of further information available, these are some of the articles we found useful in putting together this simple overview.

Coupling (computer science) – WikiPedia

Dependency Inversion – WikiPedia

Dependency Injection – WikiPedia

Dependency Injection Demystified – James Shore (Must read!)

Inversion of Control and Dependency Injection: Working with Windsor Container – MSDN

Inversion of Control Containers and the Dependency Injection Patter – Martin Fowler (reading this last)

Wednesday, January 7th, 2009



In our previous white paper Unit Testing SharePoint – Getting into the Object Model we introduced the concept of mocking, looked at the new Typemock Isolator AAA API to demonstrate testing against SPSite, SPWeb, SPList and SPListItem objects and reinforced the benefit of using TDD.

In order to keep the white paper easy to follow we used the Isolator features to fake the call to our GetRandomNumber() method as part of the test GetRandomMessage_MultipleMessages_ReturnMessage.

[Test]
public void GetRandomMessage_MultpleMessages_ReturnMessage()
{
    //Arrange
    SPSite site = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
    Isolate.Swap.NextInstance<SPSite>().With(site);
    Isolate.WhenCalled(() => site.OpenWeb().Lists["Messages"].ItemCount).WillReturn(1000);
    Isolate.WhenCalled(() => message.GetRandomNumber(1000)).WillReturn(99);
    Isolate.WhenCalled(() => site.OpenWeb().Lists["Messages"].Items[99].Title).WillReturn("Message One Hundred");
    //Act
    string rndMessage = message.GetRandomMessage("http://validsiteurl");
    //Assert
    Assert.AreEqual("Message One Hundred", rndMessage);
}

This makes for a simple test and helped us focus on the objective of testing the SharePoint object model, however this test does not work if we use Isolator for SharePoint which can only fake SharePoint objects. With this version you will get an error advising of the need to upgrade to the commercial version.

UnitTest.ObjectModel.Test.RandomMessage.GetRandomMessage_MultpleMessages_ReturnMessage:
TypeMock.TypeMockException :
*** Can not fake non-SharePoint type UnitTest.ObjectModel.RandomMessage with current license.
In order to fake non SharePoint types please upgrade to Typemock Isolator Commercial license
See more information at http://www.typemock.com/sharepointpage.php.

There are a few solutions to the problem

  1. Upgrade to the Full version of Typemock Isolator
  2. Introduce another mocking framework like NMock to mock this method
  3. Redesign you application to be more testable and make this test possible without using Typemock Isolator

NMock is an open source mocking framework that provides similar functionality to Typemock however NMock (or any other mocking framework) is unable to fake SharePoint objects as they make use of sealed classes.  We would recommend that you don’t attempt to work with two different mocking frameworks at the same time as this will make you testing much more difficult to follow and maintain.

There has been a lot of debate around Typemock being too powerful and if the value of designing testable code is as important now that Typemock can solve these issues.

We’re not going to argue the case for or against here; We’ll work on the assumption that you have Isolator for SharePoint and will redesign for testability.

Our refactoring of the project has been broken down into 3 steps proceeded by an introduction to some common vocabulary will be published over the next few days.

Sunday, January 4th, 2009



John W Powell has posted a great article on MSDN where he explains how he used Domain Driven Design (DDD), Test Driven Development (TDD) and design Patterns to create a framework for working with the SharePoint.

He goes into detail about the framework and the reasons for the final design.  I would have liked to have a little more detail, code samples, from the iterations he went through to reach this design to help people better understand how the DDD and TDD are really about designing better software.   He does say himself, so I can’t really complain

Now I’m pulling a “Julia Childs” on you. I already went through several iterations of TDD and refactoring to arrive at the implementation…

It’s really encouraging to see more of this information being published that demonstrates it is possible to do good design in SharePoint projects using agile techniques.

I have a few small areas that I disagree with, first the approach taken seems to be more test-after, in the SharePointListRepository example early on he states that “Following TDD, we’ll create a unit test that will fail until we implement the repository”.   I have no issue with the writing of tests that fail and making them pass but TDD is about driving the design and John had already made the design choice before he implemented the test.   To be fair to John this may be more about the way he wrote the blog than the code.

The Second I may being ultra critical about the naming of the tests,  but he has two tests both named GetListsTest this makes it harder to understand where a test is failing.

Personally I like to adopt a naming convention that defined exactly what is being tested and the expected results, something like:

SharePointListsServiceGateway_GetLists_ReturnList

If your getting into Behaviour Driven Development (BDD) then the naming of tests takes on an even more meaningful role.

The third and more important is the use of ‘var’ in the tests for the SharePointServiceUrl.  If you look at the tests GetServiceUriTest and GetServiceUrlEndsWithSlashTest the tests are identical, my feeling is that with the test GetServiceUriTest John was aiming to test the overloaded GetLists method that returns a Uri.   However the use of ‘var’ means that Visual Studio and the compiler make the call that this is actually a call to the method that returns a string.  If you isolate the tests in this method only you will see that this code is not called, the full code coverage however it shows 100% coverage as another test is exercising this code – a warning that just because you have 100% code coverage doesn’t mean you’ve tested your code.

The forth and last observation is probably the most important.   The tests created are Unit Tests, although if is often debated what a Unit is, one things that a Unit Test should not be is an Integration Test – however the GetLists tests have a dependency on SharePoint.  If your environment does not have a SharePoint web application matching http://contoso.com/ these tests will fail.

I have probably been too hard on John here as the emphasis of the post was about the framework,  it would however be good to make the reader aware of these and the reason for skipping them.   With that said I am still made up that people are starting to put stuff out there to help us all improve the way we develop.

Way to go John!.

PS: John was happy for me to post the comments as he wants to spark the conversations.

Sunday, December 14th, 2008



Scott Bellware posted a great article on what is often one of the main reasons for people dropping or never starting TDD or Unit Testing; often stating ‘It takes longer doing TDD’ and ‘I can code this much quicker without unit tests.’

The answer to the question, “Does test-driven development speed up development?” depends on what you personally believe “development” is.

He discusses how we should be looking at software production and how the software development business has an out-dated idea of what productivity is.

Test-driven development supports flow. The software development industry at large is years away from recognizing that flow rather than efficiency is what creates giant leaps in productivity.

Looking at this flow and the whole production pipeline he notes

Test-driven development may require you to have nerves of steel while you’re adopting it and dealing with the antithetical notion of going slower to speed up, but it will speed things up. It just might not speed you up.

I really like this concise view on why Test Driven Development does speed up development,  it just may not be what you think of as development.

Tuesday, November 25th, 2008



In our white paper Unit Testing SharePoint Solutions – Getting into the Object Model we needed to use Natural Mocks to allow the testing exceptions that are thrown as part of the the SPSite constructor.  Our test code looked like this and although not complicated it introduced another concept that could complicate things for newbies more than necessary.

[Test]
public void GetRandomMessage_SiteNotFound_ReturnErrorMessage()
{
    //Arrange
    using (RecordExpectations expections = RecorderManager.StartRecording())
    {
        expections.ExpectAndThrow(new SPSite("http://invalid_url"),  new FileNotFoundException());
    }

    //Act
    string rndMessage = message.GetRandomMessage("http://invalid_url");

    //Assert
    Assert.That(rndMessage.StartsWith("Error"));
    Assert.That(rndMessage.Contains("Site could not be found"));
}

 

With the release of V5.1.2 of Isolator for SharePoint a new API has been added to the AAA syntax to support just this scenario.

Isolate.Swap.NextInstance<T>().ConstructorWillThrow();

Our tests will now look like this:

 

[Test]
public void GetRandomMessage_SiteNotFound_ReturnErrorMessage()
{
    //Arrange
    Isolate.Swap.NextInstance<SPSite>().ConstructorWillThrow(new FileNotFoundException());

    //Act
    string rndMessage = message.GetRandomMessage("http://invalid_url");

    //Assert
    Assert.That(rndMessage.StartsWith("Error"));
    Assert.That(rndMessage.Contains("Site could not be found"));
}

You will agree this is a much easier API and helps to lower the barrier to Test Driven SharePoint development a little bit more.   We will be updating our white paper to reflect this change and updating out code over the next few days.

Update 25 Nov 08:

You will also need to add the [ClearMocks] directive to the start of you Test Fixture to ensure Isolator clears up after each test.

[TestFixture]
[ClearMocks]
public class RandomMessage

Also one of the tests will now fail if you are using the new Isolator for SharePoint with the following error:

TypeMock.TypeMockException:

*** Can not fake non-SharePoint type UnitTest.ObjectModel.RandomMessage with current license. In order to fake non SharePoint types please upgrade to Typemock Isolator Commercial license

See more information at http://www.typemock.com/sharepointpage.php.

I will be following up with the Guys at Typemock to see how much room there will be to support non SharePoint objects in the SharePoint Edition for just these scenarios.

Monday, November 24th, 2008



Another great day for SharePoint developers.   Typemock have announced the introduction of Isolator for SharePoint.

Take a look at our white paper showing Isolator in action and how this really does make Unit Testing your SharePoint solutions that much easier.

Unit Testing SharePoint Solutions – Getting into the Object Model

Quote from Typemock Blog:

Typemock are offering their new product for unit testing SharePoint called Isolator For SharePoint, for a special introduction price. it is the only tool that allows you to unit test SharePoint without a SharePoint server. To learn more click here. The first 50 bloggers who blog this text in their blog and tell us about it, will get a Full Isolator license, Free. for rules and info click here.

Wednesday, November 12th, 2008



At long last Microsoft have announced publically the excellent SPDisposeCheck tool.   This tool will examine your IL level components and report any possible areas where you may have the potential for a leak due to objects not being disposed of correctly.   The output from the tool does require a deep understanding of the way objects are disposed of as there is the potential for false positives.

For more information check out the SharePoint Team Blog even if you can’t get the tool just yet, its coming soon, the referenced posts are a must read for all SharePoint developers.

Wednesday, November 12th, 2008



The white paper and associated code can be found here Unit Testing SharePoint Solutions – Getting into the Object Model

The first document in the Beginners Guide to Test Driven Web Part Development Series provided an introduction to unit testing, a look at how TDD influences your design and makes your code testable. That document deliberately avoided testing the SharePoint Object Model as in order to do this we need to introduce the concept of mocking.

This second document in the series will cover the concept of mocking, specifically looking at the new Typemock Isolator (V5.1.1 or later) AAA API to demonstrate testing against SPSite, SPWeb, SPList and SPListItem objects.

Some of the key things you should get from this white paper are

  • How to unit test the object model
  • These unit tests are much less brittle using the new Isolator AAA API
  • You do not need a specific SharePoint configuration on your development machine, we are not Integration testing
  • Reinforce TDD approach to SharePoint development

The white paper is part of the Beginners Series but introduces some fairly advanced concepts, as with SharePoint the learning curve can be fairly steep.  You can work through this white paper on it’s own but I recommend looking at the first part as this goes into more detail about TDD.

The white paper and associated code can be found here Unit Testing SharePoint Solutions – Getting into the Object Model

We welcome you comments and suggestion for improving the current white papers and also ideas for the future.