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
Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!
0 votes
I have a method as following:

public static void Debug(string message, int eventId)
{
if (DoILog(LogLevelKey.LogVerboseMsgToEventLog))
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
Logger.Write(eventId, null, message, null, "General", 0, LogStatus.Success, TraceEventType.Verbose);
});
}
}

I need to mock the SPSecurity.RunWithElevatedPrivileges method which is a static method but it accepts a delegate as the input parameter. Inside the delegate implementation there is another static method: Logger.Write(...).

I wrote this:
SPSecurity.CodeToRunElevated inputArgument = delegate
{
Logger.Write(_eventId, null, _message, null, "General", 0, LogStatus.Success, TraceEventType.Verbose);
};
Action spSecurityAction = delegate { SPSecurity.RunWithElevatedPrivileges(inputArgument); };
Isolate.WhenCalled(spSecurityAction).IgnoreCall();

But it complains that it can't mock that static method because it has a delegate instance as input parameter.

How could I mock SPSecurity.RunWithElevatedPrivileges?

Thanks
asked by (3k points)

3 Answers

0 votes
Hi Ben,

You can fake all static methods of SPSecurity to prevent that method from running:

[TestMethod]
public void SPSecurityRunWithElevatedPrivileges_IgnoreCall_CallIsIgnored()
{
    Isolate.Fake.StaticMethods(typeof(SPSecurity));

    bool wasCalled = false;
    SPSecurity.RunWithElevatedPrivileges(() => { wasCalled = true; }); 
    Assert.IsFalse(wasCalled);
}


Please let me know if this helps.

Thanks,
Doron
Typemock Support
answered by (16.5k points)
0 votes
Hi Doron,

Thanks for your response.

Well, I know that but I need to ensure that the right arguments has been passed to the RunWithElevatedPrivileges() static method.

Is there any way to achieve this? Faking the implementation of a method makes sense but ignoring the arguments doesn't, as we should be able to validate that.

Thanks.
answered by (3k points)
0 votes
There's an older syntax (Natural Mocks) that they use for this sort of thing:

https://www.typemock.com/Docs/UserGuide/newGuide/Documentation/CheckArgumentsNatural.html

Being able to check specifics about individual arguments in AAA is coming soon from what I'm see in this forum...

Hope that helps.
answered by (21.8k points)
...