After lots of searching and testing, I finally ended up with piece of code to mock a user for unit testing.
this.Principal = new Mock<IPrincipal>(); List<Claim> claims = new List<Claim>{ new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", username), new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", userid) }; var fakeIdentity = new GenericIdentity(username); //fakeIdentity.RemoveClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", "")); fakeIdentity.AddClaims(claims); var fakePrincipal = new GenericPrincipal(fakeIdentity, roles); Principal.Setup(n => n.Identity).Returns(fakeIdentity); Http.Setup(x => x.User).Returns(fakePrincipal); Http.Setup(x => x.User.Identity.Name).Returns(username); Http.Setup(x => x.User.Identity).Returns(fakeIdentity);
No comments:
Post a Comment