Test code in Elevated privileges

25/03/2013 13:34

[TestInitialize]

public void TestSetUpSettings()

{

    target = new CustomAscxUserControlHelper();

 

    //Enable RunWithElevated privileges

    var spSecMock = MockManager.Mock(typeof(SPSecurity));

    spSecMock.MockMethodCalled += SpSecMockMockMethodCalled;

    spSecMock.ExpectAlways("RunWithElevatedPrivileges");

}

static void SpSecMockMockMethodCalled(object sender, MockMethodCallEventArgs e)

{

    if (e.CalledMethodName.Equals("RunWithElevatedPrivileges"))

    {

        var del = (SPSecurity.CodeToRunElevated)e.SentArguments[0];

        var res = del.BeginInvoke(null, null);

        res.AsyncWaitHandle.WaitOne();

        del.EndInvoke(res);

    }

}