How to mock IAuthorizationService in .net core 2.0

I use this resource authorization in my controller:

var result = await _authorizationService.AuthorizeAsync(User, document, operation); 

I need to check my controller, and I need to go through authorization to pass the test.

I tried:

 _substituteAuthorizationService.AuthorizeAsync(Arg.Any<ClaimsPrincipal>(), null, Arg.Any<IEnumerable<IAuthorizationRequirement>>()) .ReturnsForAnyArgs(new AuthorizationResult(......)); 

but I cannot create a new AuthorizationResult because it does not have a public constructor.

Any ideas? Thanks!

+5
source share
1 answer

Not so detailed here https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authorization.authorizationresult?view=aspnetcore-2.0

But you can return from static method (s)

 AuthorizationResult.Success() AuthorizationResult.Failed() 
+4
source

Source: https://habr.com/ru/post/1272864/


All Articles