How to add claims in a mock ClaimsPrincipal

后端 未结 2 452
悲哀的现实
悲哀的现实 2020-12-15 02:50

I am trying to unit test my controller code which gets the information from the ClaimsPrincipal.Current. In the controller code I

public class HomeControlle         


        
2条回答
  •  -上瘾入骨i
    2020-12-15 03:26

    You don't need to mock ClaimsPrincipal it has no outside dependencies and you can created it un-mocked:

    var claims = new List() 
    { 
        new Claim(ClaimTypes.Name, "username"),
        new Claim(ClaimTypes.NameIdentifier, "userId"),
        new Claim("name", "John Doe"),
    };
    var identity = new ClaimsIdentity(claims, "TestAuthType");
    var claimsPrincipal = new ClaimsPrincipal(identity);
    

    And I'm not sure what you are testing here. Certainly "John Doe" will not be part of viewResult.Content because it is never been set to this.

提交回复
热议问题