I've inherited a SharePoint project and I'm at the step when I will start adding tests for regression testing. The fact that practically every method in the project relies on SPSite and similar SP... classes means that I have to mock SharePoint classes within the tests.

Microsoft's SharePoint.Emulators library is not an option, since the project uses .NET Framework 4.5. On the other hand, SPEmulators works well.

The only problem is that as soon as the code goes a bit deeper within SharePoint structure, it hits nulls within the mocks. For instance, this is the beginning of the method I try to test right now:

public void Demo(SPSite site)
    List<SPAlternateUrl> urls = site.WebApplication.AlternateUrls.ToList();

Site exists—this is the actual emulated object—and so does site.WebApplication, but site.WebApplication.AlternateUrls is null, so the method throws an exception as soon as it begins.

Since the tests are done like this:

using (var context = new SPEmulationContext(IsolationLevel.Fake))

there is no way to change the objects at their initialization (like in Microsoft Fakes), and since AlternateUrls is readonly, I suppose that it cannot be changed later either.

One option is to modify the method to do the needed checks. I don't want to start changing the code which has currently 0% branch coverage.

What are my options?

How is done the regression testing of legacy code which relies too much on SP... classes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.