Book Image

Web Development with MongoDB and Node.js

By : Jason Krol
Book Image

Web Development with MongoDB and Node.js

By: Jason Krol

Overview of this book

Table of Contents (19 chapters)
Web Development with MongoDB and Node.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
12
Popular Node.js Web Frameworks
Index

Spy and stub everything!


When in doubt, the safest thing you can do when writing your tests is spy on everything and stub everything else. There are always going to be times that you'll want a function to execute naturally; in that case, leave it alone. Ultimately, you never want your tests to be dependent on any other system—that includes database servers, other network servers, other APIs, and so on. You only want to test that your own code works, nothing more. If your code is expected to make a call to an API, spy on the actual call and just assert that your code attempted to make the call. Likewise, fake the response from the server via a stub and ensure that your code handles the response properly.

The easiest way to check for dependencies in your code is to stop any other services from running (your local node app, and so on), as well as possibly even disabling your network connection. If your tests timeout or fail somewhere unexpectedly, it's likely because you missed a function you...