Unit testing MOSS part 2 (of 2)

In the 1. article on Unit testing MOSS I described some of the findings and experience on unit
testing SharePoint applications using the TypeMock Isolator framework. In this 2. article I will
provide some examples of unit tests.
 
Code under test
You should of course use TDD principles, but we are in a state where we have to add unit tests to already
existing code.

The code under test is a fairly simple method, already existing, which main purpose is to set the
MasterUrl-property on a site:

    public class CodeUnderTest
    {
        public void SetMasterUrlProperty(SPWeb web, string masterUrl)
        {
            try
            {
                if (masterUrl != null)
                {
                    if (!masterUrl.ToLower().EndsWith(".master"))
                    {
                        throw new ApplicationException("Invalid MasterUrl");
                    }
                    web.MasterUrl = masterUrl;
                    web.Update();
                }
            }
            catch (ApplicationException ex)
            {
                LogError("Unable to set master url property", ex);
            }
        }
    }

 

The tests
After creating a test project using right-click on the method, a test project, class and method 
is provided for modification. The tests will by default fail with a "test inconclusive" error
until it has been modified.

 

Success scenario

First we create the successful test where all is working as it should:

public void SetMasterUrlTest_Success()
{
    //Arrange
    CodeUnderTest codeUnderTest = new CodeUnderTest();
    web = Isolate.Fake.Instance<SPWeb>(Members.ReturnRecursiveFakes);
    string masterUrl = "test.master";
    Isolate.WhenCalled(() => web.MasterUrl).WillReturn("test.master");
    
    //Act
    codeUnderTest .SetMasterUrlProperty(web, masterUrl);
    
    //Assert
    Isolate.Verify.WasCalledWithAnyArguments(() => web.Update);
}

 

Here we are using the Arrange-Act-Assert pattern where we:
1.create an instance of the class under test
2.set up a fake SPWeb object
3.setting behavior on the .MasterUrl property
4.calling the method under test
5.verification that the Update method is called

 

Parameter testing
The next test we do is checking how the method under test is behaving if we provide a null-value as MasterUrl parameter.
       
        public void SetMasterUrlTest_MasterUrlIsNull()
        {
            //Arrange
            CodeUnderTest codeUnderTest  = new CodeUnderTest ();
            string masterUrl = null;
            Isolate.WhenCalled(() => web.MasterUrl).WillThrow(new System.Exception("MasterUrl should be set to NULL"));
            try
            {

                //Act
                codeUnderTest.SetMasterUrlProperty(web, masterUrl);
            }
            catch
            {
                 //Assert
                 Assert.Fail();
            }
        }

Here we are telling the method under test to throw an exception if MasterUrl is called with a null-value in the MasterUrl property. In other words, it should be allowed to provide a null-value as a parameter and the call should be done. We are expecting the test to fail if the property is called with the Assert.Fail in the catch-block.
       
Exception flow testing

The last test is a test to see how the method under test is behaiving when provided an invalid MasterUrl value:

       

        public void SetMasterUrlTest_InvalidUrl()
        {
            //Arrange
            CodeUnderTest codeUnderTest = new CodeUnderTest();
           
            string masterUrl = "ASDFASDF";
            Isolate.WhenCalled(() => web.MasterUrl).WillThrow(new System.Exception("Invalid MasterUrl"));
            try
            {
                //Act

                codeUnderTest.SetMasterUrlProperty(web, masterUrl);

                //Assert
                Assert.Fail();
            }
            catch
            {
            }
        }

Here we are expecting the method under test to throw an exception if an invalid MasterUrl-value is provided.
 

 

Happy testing!