Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to mock AuthenticationStateProvider

$
0
0

I have a class that takes an AuthenticationStateProvider in the constructor. I'd like to write unit test and mock this.

I'd like to be able to set what user is returned from the call GetAuthenticationStateAsync.

  const string userId = "123";  const string userName = "John Doe";  const string email = "john.doe@test.com";  var claims = new List<Claim>  {    new Claim(ClaimTypes.NameIdentifier, userId),    new Claim(ClaimTypes.Name, userName),    new Claim(ClaimTypes.Email, email), }; var identity = new Mock<ClaimsIdentity>(claims); var principal = new Mock<ClaimsPrincipal>(identity.Object); var mockOfAuthenticationStateProvider = new Mock<AuthenticationStateProvider>(); var mockOfAuthState = new Mock<AuthenticationState>(); mockOfAuthenticationStateProvider.Setup(p =>    p.GetAuthenticationStateAsync()).Returns(Task.FromResult(mockOfAuthState.Object)); 

I get this errormessage:

Testfunction threw exception: System.ArgumentException: Can notinstantiate proxy of class:Microsoft.AspNetCore.Components.Authorization.AuthenticationState.Could not find a parameterless constructor. (Parameter'constructorArguments') ---> System.MissingMethodException:Constructor on type 'Castle.Proxies.AuthenticationStateProxy' notfound.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>