質問

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!

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top