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.