Search code examples
unit-testingdependency-injectionaspnetboilerplate

aspnet zero - replace IPermissionChecker for Test Module


I am creating another test module for an application service which is using different DbContext.

Is there any way to bypass permission checking for the application service in Unit Test?

I've tried

Configuration.ReplaceService<IPermissionChecker, NullPermissionChecker>(DependencyLifeStyle.Transient);

in Test Module PreInitialize() but it is still checking the permissions.

Please help! Thank you!


Solution

  • services.AddAbpIdentity replaces IPermissionChecker again. You can use AddPermissionChecker<NullPermissionChecker> extension method after services.AddAbpIdentity.

    For AspNet Zero, it's inside IdentityRegistrar class. Example (add the last line):

    services.AddAbpIdentity<Tenant, User, Role>(options =>
        {
            ...
        })
        ...
        .AddPermissionChecker<NullPermissionChecker>();
    

    Notice that: You probably want to make this conditional and apply only for unit tests.