Skip to content

Instantly share code, notes, and snippets.

@Ibro
Last active February 24, 2018 09:42
Show Gist options
  • Save Ibro/1eed7d987f66b8974d17da10fa1333f0 to your computer and use it in GitHub Desktop.
Save Ibro/1eed7d987f66b8974d17da10fa1333f0 to your computer and use it in GitHub Desktop.
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