How to use the mocks and stubs with multiple API requests in Express.js using Sinon and Mocha

  • 2 months ago
The mocks and stubs and essential features in the test frameworks. Them are used to create fake objects, functions and isolate real requests(DB, API, etc.).

This video shows a real example of code with multiple API requests and how I'm using mocks and stubs to test it.
Transcript
00:00For Google sign-in in MyAuthenticationService, I'm using the official API.
00:10Initially, the user must grant permissions to MyProject to access their data.
00:20Subsequently, Google redirects the user to a page containing the authorization code in the URL.
00:32In MyAuthenticationService, I'm using Next.js for the frontend.
00:40The authorization code is passed to the backend where a dedicated function handles it.
00:56In this function, I'm using the authorization code GoogleProjectCredentialsRedirectUri
01:12to exchange the authorization code for an access token.
01:22If the response is successful and I have obtained the access token,
01:34I will use the access token to request the user data, like ID and email.
01:52Here I'm using the Mongoose user model to search in MongoDB the user ID.
02:06If a user is found, it means the user is registered.
02:18I will generate a JWT token and return the user's data along with an access token.
02:40With this data, the user will be redirected to the dashboard page.
02:50If the user is not found, I will return the user ID from the Google API and the email.
03:14The user will be redirected to the registration page where will be asked to enter a password and create an account.
03:30Now, the question is how to test this code.
03:36I have created a test file and I'm using Rhino to build the tests.
03:50In my test file, I have created two mocks, one for ExpressRequest object and second for ExpressResponse object.
04:19These mocks will provide fake objects.
04:26I need to somehow isolate these requests.
04:44For this purpose, I have created four tabs and after each request, I'm restoring them.
05:04In my test case, I need to add a fake authorization code to my mocked request object
05:25because the function under test relies on it.
05:39In my test case, I will use even a mock for response because it is used even in my function and it will receive the response.
06:09For mocks, this is used for the request which exchanges the authorization code for an access token.
06:26I need to verify which field contains the response and set a response in this tab.
06:41This is valid even for the tab which replaces the request for user information.
06:55I need to verify which field returns the request and provide in this tab a response.
07:11This is for Mongo's user model.
07:20It returns user ID and email.
07:31If a user is found, I need to provide even a AWT token.
07:44Now, let's run a test.
08:01It works as expected, but this test is valid only for the sign-in.
08:17How about registration?
08:20For registration, I need to change the value for the tab which replaces the Mongo's user model.
08:51I need to change the value for the tab which replaces the Mongo's user model.
08:59I need to change the value for the tab which replaces the Mongo's user model.
09:08I need to change the value for the tab which replaces the Mongo's user model.
09:19Even this test is working as expected.
09:29Let's run a test.
09:36It works as expected, but this test is valid only for the sign-in.
09:45How about registration?

Recommended