我在这个帖子上看了非常好的内容,它并没有解决我的问题。MSTEST PrincipalPermission
我的课:
public class SecurityUsingAttributes
{
[PrincipalPermission(SecurityAction.Demand, Role = "SomeRole")]
public int MyMethod1()
{
return 5;
}
}
我的测试:
[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());
}
}
这现在有效。