Instantly share code, notes, and snippets.

Embed
What would you like to do?
BuildAuthorizationService - ASP.NET Core - Unit Testing Authorization Service - https://codingblast.com/asp-net-core-unit-testing-authorizationservice-inside-controller/
var mockMyRepository = new Mock<IMyRepository>();
mockMyRepository.Setup(repo => repo.GetData("john"))
.Returns(Task.FromResult("Corrrect"));
mockMyRepository.Setup(repo => repo.GetData("codingblast"))
.Returns(Task.FromResult("Yeah!"));
var authService = BuildAuthorizationService(services =>
{
services.AddScoped<IMyRepository>(sp => mockMyRepository.Object);
services.AddScoped<IAuthorizationHandler, MyCustomHandler>();
services.AddAuthorization(options =>
{
options.AddPolicy("PolicyName", policy => policy.Requirements.Add(new MyCustomRequirement()));
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment