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?