Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock IAuthorizationService in .net core 2.0

I'm using this resource authorization in my controller:

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

I need to test my controller, and I need the authorization to pass in the test.

I tried:

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

but I can't new an AuthorizationResult because it doesn't have a public constructor.

Any ideas? Thanks!

like image 1000
John-Luke Laue Avatar asked Oct 25 '17 01:10

John-Luke Laue


1 Answers

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

But you can return from the static method(s)

AuthorizationResult.Success()
AuthorizationResult.Failed()
like image 178
Jasen Avatar answered Sep 22 '22 17:09

Jasen