Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6338301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:24:59+00:00 2026-05-24T19:24:59+00:00

I want to be able to mock the object that is returned by SPServer.Local

  • 0

I want to be able to mock the object that is returned by SPServer.Local but I can’t seem to do it in typemock. At the moment when I debug, I see that SPServer.Local returns a null object of type SPServer. Shouldn’t typemock be swapping out this instance with my fake instance? Is there something I’m doing wrong? The code runs fine on the sharepoint server.

[TestInitialize]
public void Setup()
{
    fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
    Isolate.Swap.NextInstance<SPSite>().With(fakeSite);
    fakeServer = Isolate.Fake.Instance<SPServer>(Members.ReturnRecursiveFakes);
    Isolate.Swap.NextInstance<SPServer>().With(fakeServer);
    sharePointStorageRepository = new SharePointStorageRepository();
}


[TestMethod]
[Isolated]
public void CreateHRFolderMethodCreatesHRFolder()
{
    // arrange
    // some arrange logic here

    // act
    var actual = sharePointStorageRepository.Create();

    // assert
    Assert.AreEqual(expected, actual);
}

This is the bit of code that is being run:

internal static Guid GetSiteGuid(string serverRelativeUrl, string webApplicationName)
{
    Guid? guid = null;
    SPServer myServer = SPServer.Local;
    foreach (var serviceInstance in myServer.ServiceInstances.Where(si => si.Service is SPWebService)){
        var service = (SPWebService) serviceInstance.Service;
        var webapp = service.WebApplications.SingleOrDefault(wa => wa.DisplayName == webApplicationName);
        if (webapp != null){
            var site = webapp.Sites.SingleOrDefault(wa => wa.ServerRelativeUrl == serverRelativeUrl);
            if (site != null) guid = site.ID;
        }
    }

    if (!guid.HasValue){
        throw new FileNotFoundException(
            String.Format(
                "Cannot find Site Collection with WebApplication \"{1}\" and ServerRelativeUrl \"{2}\" running on \"{0}\"",
                myServer.Address, webApplicationName, serverRelativeUrl));
    }

    return guid.Value;
}

Thanks all!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:25:01+00:00Added an answer on May 24, 2026 at 7:25 pm

    I don’t work in SharePoint, but something I noticed: You’re not actually mocking the return of SPServer.Local anywhere. I think that’s the missing step. I’m also not entirely sure you need to SwapNextInstance since I don’t see anywhere that is actually creating an SPServer object.

    That would change your test code to:

    [TestInitialize]
    public void Setup()
    {
        // I don't see where you're using SPSite, so I assume it's in code
        // not being shown; otherwise you can remove this.
        fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
        Isolate.Swap.NextInstance<SPSite>().With(fakeSite);
        fakeServer = Isolate.Fake.Instance<SPServer>(Members.ReturnRecursiveFakes);
    
        // INSTEAD OF THIS: Isolate.Swap.NextInstance<SPServer>().With(fakeServer);
        // DO THIS:
        Isolate.WhenCalled(() => SPServer.Local).WillReturn(fakeServer);
    
        sharePointStorageRepository = new SharePointStorageRepository();
    }
    

    That WhenCalled method will mean that any time anyone asks for SPServer.Local, it’ll return your fake instance.

    Note that I see in the code being tested that you get the ServerInstances property. I don’t see any specific return values getting set up, so I assume you’re controlling the rest of the stuff in the omitted “arrange” logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a mock data source that I want to be able to pass
I want to be able to capture the exception that is thrown when a
I want to be able to spec out that when Open-Uri open() calls either
I want to be able to have a Settings class that is available throughout
I've got a PHPUnit mock object that returns 'return value' no matter what its
We have a URL object in one of our Java classes that we want
I have a textReader that in a specific instance I want to be able
Using EasyMock I want to be able to say that I expect a specific
Is it possible to mock an object in a way that it fakes multiple
I'm using LaTeX and BibTeX for an article, and I want to able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.