This is a migrated thread and some comments may be shown as answers.

Mocking SharePoint SPSecurity.RunWithElevatedPrivileges

5 Answers 48 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Daniel Eggers PO 4900000041
Top achievements
Rank 1
Daniel Eggers PO 4900000041 asked on 20 Oct 2010, 04:04 PM
Hi,

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

Sort by
0
Ricky
Telerik team
answered on 22 Oct 2010, 09:45 AM
Hi Robert,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Robert
Top achievements
Rank 1
answered on 22 Oct 2010, 03:04 PM
Hi,

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
0
Ricky
Telerik team
answered on 25 Oct 2010, 12:15 PM
Hello 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

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Robert
Top achievements
Rank 1
answered on 25 Oct 2010, 02:35 PM
Prefect, that's exactly what I was looking for!

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
0
Ricky
Telerik team
answered on 27 Oct 2010, 10:27 AM
Hi 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
 
Tags
General Discussions
Asked by
Daniel Eggers PO 4900000041
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Robert
Top achievements
Rank 1
Share this question
or