
is there any way to mock the the call to SPSecurity.RunWithElevatedPrivileges delegate, e.g.
...
SPSecurity.RunWithElevatedPrivileges(
delegate
()
{
// Code to unit test
});
...
Basically, I'd like to test the code passed as an anonymous delegate to the SPSecurity.RunWithElevatedPrivileges(), but am not able to mock the call to the SPSecurity.RunWithElevatedPrivileges() to simply bypass SharePoint and just execute whatever is passed in.
Any suggestions?
Regards, Robert
5 Answers, 1 is accepted
Thank you for sending the issue. I attached a test project that contains the following example of mocking SPSecurity.RunWithElevatedPrivileges. I have used JustMock SP1 for the purpose.
bool
called =
false
;
Mock
.Arrange(() => SPSecurity.RunWithElevatedPrivileges(Arg.IsAny<SPSecurity.CodeToRunElevated>()))
.DoInstead(() => called =
true
);
SPSecurity.RunWithElevatedPrivileges(
delegate
()
{
// nothing.
});
Assert.IsTrue(called);
Here the issue could be that the delegate that you are passing in Mock.Arrange does not match with the one that is passed during actual execution. The solution is either to use Arg.IsAny<> or assign the expected delegate in a variable and then pass it in both places.
Please take a look at the project and let us know if you are still having problem.Kind Regards,
the Telerik team

thanks for the info. However, in this case I can only tell if the delegate was called, but cannot test the delegate itself. Can I somehow tell the DoInstead() method to execute whatever delegate was passed in?
For instance, I'd like to test that the following method indeed returns 5:
public
int
Return5()
{
int
a = 0;
SPSecurity.RunWithElevatedPrivileges(
delegate
()
{
a = 5;
});
return
a;
}
For this, I'd need to mock out the SPSecurity.RunWithElevatedPrivileges and execute the anonimous delegate.
Any way to achieve this?
Regards, Robert
Thanks again for the reply and yes it is possible to execute the calling delegate inside DoInstead. I have updated the test method in attached project in the following way to mimic the scenario where a value is set to a local variable that you want to return or that is expected when
SPSecurity.RunWithElevatedPrivileges
is called.
Mock
.Arrange(() => SPSecurity.RunWithElevatedPrivileges(Arg.IsAny<SPSecurity.CodeToRunElevated>()))
.DoInstead((SPSecurity.CodeToRunElevated elevated) => elevated());
int
a = 0;
SPSecurity.RunWithElevatedPrivileges(
delegate
()
{
a = 5;
});
Assert.AreEqual(5, a);
Please feel free to write back, if you have further questions on this matter.
Kind regards,
the Telerik team

The DoInstead method is highly underdocumented, you should include samples like this in the documentation to make it clear how to use the parameters passed to the original call in the DoInstead().
Thanks,
Robert
Thanks again for the reply. It is great to hear that things worked for you. Regarding the DoInstead() documentation , I created a backlog entry for including similar samples on how to use the parameters passed to the original call in DoInstead(). Hopefully it will be updated to the documentation as soon as possible.
Also, please feel to write back if you have further questions.
Kind Regards,the Telerik team