0

I've looked at the very good content at this thread and it hasn't solved my problem. MSTEST PrincipalPermission

My class:

public class SecurityUsingAttributes
{
    [PrincipalPermission(SecurityAction.Demand, Role = "SomeRole")]
    public int MyMethod1()
    {
        return 5;
    }
}

My test:

[TestClass]
public class SecurityUsingAttributesTests
{
    [TestMethod]
    public void TestMethod1()
    {
        IIdentity identity = new GenericIdentity(@"MyDomain\MyName");
        string[] roles = new string[] { "SomeRole"};

        IPrincipal genericPrincipal = new GenericPrincipal(identity, roles);


        Thread.CurrentPrincipal = genericPrincipal;

        SecurityUsingAttributes target = new SecurityUsingAttributes();

        Assert.IsTrue(5 == target.MyMethod1());
    }
}

This works now.

Community
  • 1
  • 1
RT.
  • 435
  • 2
  • 9
  • 25

1 Answers1

0

I was missing the assignment for CurrentPrincipal. The above code works now.

RT.
  • 435
  • 2
  • 9
  • 25