Building JWT unit tests
All our models do is read and write to the database. This has been shown to work. The only module left that we’ll unit test is the auth
module. Here, we have some logic that has multiple outcomes based on the inputs. We also must do some mocking as some of the functions accept actix_web
structs, which have certain fields and functions. Luckily for us, actix_web
has a test module that enables us to mock requests.
Building a configuration for tests
Before we start building our unit tests for the JWT, we must remember that there is a dependency on the config
file to get the secret key. Unit tests must be isolated. They should not need to have the correct parameters passed into them to work. They should work every time in isolation. Because of this, we are going to have to build a new
function for our Config
struct in our src/config.rs
file. The outline for the coding tests will look like the following code:
impl Config { ...