Of course with this method, the process of running the tests, iterating on them, and setting up the environment is slower.Finally, the integration between the UserService and the MongoDB driver is being tested, meaning if a breaking change is introduced, tests can catch it.As noted before it's good to be able to read the tests and understand the code's behavior and functions/methods signatures. Tests are more realistic and close to the actual use of our code.As you can see tests are much simpler and straightforward to write.That's wrong and can be misleading to someone reading the tests. In the below example, you can notice the createClient is returning a string. Finally, tests can be a really good guide on how a function or a method is used and its signature.If a new breaking change is introduced to the MongoDB driver, you run the risk of not catching those breaking changes as your tests don't interact with the driver.More times than I would like to admit my code misbehaves because of badly written or complex mocks. Another problem with this method is that tests are relying a lot on the mocks.It's clear in the complete example that mocks are more lines of code than the actual UserService. Includes much more code, other than the actual implementation, creating the extra mocks.Speed, once mocks are ready, it's fast to write and run the tests.Makes the tests more reliable as they don't rely on HTTP requests or connections with MongoDB in this case.This method gives us the ability to test the code that we have written in isolation of other factors like the MongoDB driver.We need to identify what methods from the MongoDB driver are being used by theĬode and create mocks that emulate the functionality and assert that the code behaves as it should. With the same pattern, we can mock and verify all the methods are called with the correct arguments. Import Įnter fullscreen mode Exit fullscreen mode The code provided is written in Typescript but it's not a lot different from being in Javascript. MongoDB In-Memory Server for mocking Mongodb.Docker and docker-compose for setting locally Mongodb.Use a mock MongoDB with Mongodb-In-Memory-Server.Spinning a dedicated MongoDB instance with docker-compose for running the tests.I am going to show 3 different ways for testing: The second reason is for showing how we should write tests in isolation, meaning there should be no external interference that could probably skew test results. The first reason for writing this article is to show that except the benefits of having your code tested ( confidence in the quality of your code, catch bugs before pushing code, etc ) it can also be quite as entertaining and educational as writing the actual code. The motivation for writing this article is twofold. Write tests for your application to make sure the code you write will do what it is supposed to do. One of the main benefits of MongoDB is how flexible it is and how fast you can bring value. MongoDB is one of the most popular databases right now and many people use it as primary storage on their applications in their pet projects or even in big production applications.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |