chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi,

I'm currently TypeMock Isolator for SharePoint. I've already read the example unit test which mostly address SPSite, SPWeb, SPList and SPListItem classes.

Now I'm digging a little bit deeper, trying to fake other object types.

So I wrote following little class for which I want to do a unit test. The class consists of a constructor which takes the URL of a SharePoint web application and a method which returns the managed paths defined for this web application in a list.

public class Logic
    {
        private SPWebApplication webApp;

        public CourseSiteCreation(string webAppURLString)
        {            
                Uri webAppURL = new Uri(webAppURLString);
                webApp = SPWebApplication.Lookup(webAppURL);            
        }

        public List<string> GetManagedPathsList()
        {
            List<string> retList = new List<string>();
            SPPrefixCollection prefixes = webApp.Prefixes;  

            foreach (SPPrefix prefix in prefixes)
            {
                retList.Add(prefix.Name);
            }
            return retList;
        }


To test this method I want to fake the SPWebApplication webApp within the Logic class so that it will return 3 maneged paths when the GetManagedPathsList method is tested.

My unit test method looks like this:

 [Test]
 [Isolated]
 public void GetManagedPaths_RequestAllManagedPaths_ValidAmountOfPaths()
        {
            SPWebApplication fakeWebApp = Isolate.Fake.Instance<SPWebApplication>
           (Members.ReturnRecursiveFakes);

            Isolate.Swap.NextInstance<SPWebApplication>().With(fakeWebApp);
            SPPrefix fakedPrefix = Isolate.Fake.Instance<SPPrefix>(Members.ReturnRecursiveFakes);             
            Isolate.WhenCalled(() => fakeWebApp.Prefixes).WillReturnCollectionValuesOf(new[]
                             {fakedPrefix, fakedPrefix, fakedPrefix});

            Logic webApp = new Logic ("http://www.dummy.com");

            List<string> list = webApp.GetManagedPathsList();

            Assert.AreEqual(3, list.Count);
        }



When I run this unit test a System.NullReferenceException is thrown at the line starting with "Isolate.WhenCalled(..." .

What am I doing wrong?
asked by psysp (600 points)

1 Answer

0 votes
Hi psysp,

Sorry about not getting back to you earlier.
The problem is that the code under test is dependant on SPWebApplication.Lookup(), which is an external dependency in Sharepoint.dll. In order to Isolate this dependency you need to fake it. Furthermore, SPWebApplication.Lookup() is the method used to retrieve your fakeWebApp, so the way it should be faked is simply to return the fakeWebApp (the Swap.NextInstance() statement is redundant, you only need that to replace instances created with a 'new' statement):
[TestMethod]
public void GetManagedPaths_RequestAllManagedPaths_ValidAmountOfPaths()
{
    var fakeWebApp = Isolate.Fake.Instance<SPWebApplication>(Members.ReturnRecursiveFakes);

    SPPrefix fakedPrefix = Isolate.Fake.Instance<SPPrefix>(Members.ReturnRecursiveFakes);
    // fake the Lookup() method to return our fake web app
    Isolate.WhenCalled(() => SPWebApplication.Lookup(null)).WillReturn(fakeWebApp);
    Isolate.WhenCalled(() => fakeWebApp.Prefixes).WillReturnCollectionValuesOf(new[] { fakedPrefix, fakedPrefix, fakedPrefix });

    Logic webApp = new Logic("http://www.dummy.com");

    List<string> list = webApp.GetManagedPathsList();

    Assert.AreEqual(3, list.Count);
}


Please let me know if this resolved the issue.

Thanks,
Doron
Typemock Support
answered by doron (17.2k points)
...