Mock database
How to mock MongoDB integration
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the Pain Free Mocking with Jest course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Pain Free Mocking with Jest, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[00:00 - 00:18] In this lesson, we'll explore how to mock a database connection using Jest and the popular MongoDB library Mongoose. By mocking the database, we can run our test in an isolated manner and ensure reliable and consistent results.
[00:19 - 00:35] We discussed the two approaches to unit testing earlier. One approach argues that tests you run in isolation and all its dependencies or collaborators should be mocked. The other approach argues that it's not the system on the test that needs to be run in an isolated manner.
[00:36 - 00:53] Rather, we need to isolate tests from each other by mocking their shared state like a database. Both agreed to an extent that unit tests need to be isolated. The extent to which we need to isolate tests is where both approaches differ.
[00:54 - 01:08] First, let's install the required dependencies. We'll need to install Mongoose, which is a popular MongoDB library for Node.js. You can install Mongoose by running the following command in your terminal.
[01:09 - 01:36] Once we have installed Mongoose, we need to create a user model. This model represents a user object in our application and provides an interface to interact with the database. In the source folder, create a new directory, "Models", and in the "Models", create a new file, "User.js" file.
[01:37 - 01:50] In this example, we've defined a user model using Mongoose, KIMA, and model functions. This model allows us to perform operations on user data such as saving and retrieving users from the database.
[01:51 - 02:01] Now that we have our user model set up, let's create the "Create user" function in our user service file. This function will accept the user object and save it to the database using the user model.
[02:02 - 02:21] In this example, create user function as a user object and uses the user model 's "Save" method to save the user to the database. This is a simplified implementation for demonstration purposes. Thankfully, there's a library called "Mocking Goose" that simplifies the process of mocking "Mongose" models.
[02:22 - 02:35] To use "Mocking Goose", we need to install it as a dependency. Now let's write our "Create user" function test using "Mocking Goose".
[02:36 - 02:49] With "Mocking Goose", we can easily mock the behavior of "Mongose" models without the need to manually stop being "Mocking". In this example, we're using "Mocking Goose" user.to return to stop the "Save" method and define the expected behavior.
[02:50 - 03:02] This allows us to test our "Create user" function in a more concise and readable way. Over here, I've created the user object and also here I created the "DB" user.
[03:03 - 03:16] This is similar to how the user will be saved in the database. Using "Mocking Goose" whenever the application calls the "Save" method on the user model, it will return the "DB" user that was created here.
[03:17 - 03:29] In summary, "Mocking a database connection" is essential for running isolated and reliable unit tests. With tools like "Just" and "Mocking Goose", we can easily mock "Mongose" models and their behavior.
[03:30 - 03:38] By mocking the database, we can focus on testing our code without worrying about the complexities of the actual database interactions.