Skip to content

Unit Test your AuthP app

Jon P Smith edited this page Jan 3, 2023 · 4 revisions

If you want to unit test your application code, and that code relies on the AuthP features you have some options:

  1. Most services have an interface, so you can stub out the interface with your own code. This is the best way to unit test your application.
  2. You can use AuthP's SetupForUnitTestingAsync extension at the end of the configuration of an AuthP setup. This is much more complex, but you are now testing against the actual AuthP library.

Stubbing is a known approach, but if you really want to against the actual AuthP library, then read the following information.

Setting up AuthP in an unit test.

You would create the same sort of setup as you have in your application, but with these differences:

  • I suggest you use an in-memory database, i.e. use the UsingInMemoryDatabase extension method.
  • You might want to seed the AuthP's database with by using the Bulk Loading feature to quickly set up the AuthP parts of the application. Alternatively you could use the admin services after the setup.
  • The last method in the setup of the AuthP code should be SetupForUnitTestingAsync. This will:
    1. Build the ServiceProvider which contains all the AuthP's services (which is returned).
    2. Then it creates the AuthP database using EF Core's EnsureCreatedAsync method.
    3. It applied any Bulk Load methods to seed the AuthP's database.
    4. It returns the built ServiceProvider so that can access the services in the AuthP library.

Example of how to set up a unit test

Look at the UnitTestExample for an example of using the SetupForUnitTestingAsync method to set up AuthP for testing.

NOTE: Other unit tests in that folder have more complex unit tests - check them out if you need a deeper look inside your application using AuthP.

Articles / Videos

Concepts

Setup

Usage

Admin

SupportCode

Clone this wiki locally