Mocking SharePoint SPSecurity.RunWithElevatedPrivileges

6 posts, 0 answers
  1. Daniel Eggers PO 4900000041
    Daniel Eggers PO 4900000041 avatar
    3 posts
    Member since:
    Nov 2009

    Posted 20 Oct 2010 Link to this post

    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
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 22 Oct 2010 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Robert
    Robert avatar
    6 posts
    Member since:
    Aug 2012

    Posted 22 Oct 2010 Link to this post

    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
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 25 Oct 2010 Link to this post

    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
  6. Robert
    Robert avatar
    6 posts
    Member since:
    Aug 2012

    Posted 25 Oct 2010 Link to this post

    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
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 27 Oct 2010 Link to this post

    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
     
Back to Top
DevCraft R3 2016 release webinar banner