When unit testing real-world code, there are many situations that make tests hard to write. How do you check if a function was called? How do you test an Ajax call? Or code using setTimeout? That’s when you use test doubles — replacement code that makes hard to test things easy to test. For many years, Sinon.js has been the …
5 step method to make test-driven development and unit testing easy
What’s the hardest thing in test-driven development or unit testing in general? Writing tests! The syntax or the tools aren’t the problem – you can learn those enough to get started in 15 minutes. The problem is how to take some vague idea in your head about what you want to do, and turn it into something that verifies some …
Mongoose models and unit tests: The definitive guide
A korean translation of this article Mongoose is a great tool, as it helps you build Node apps which use MongoDB more easily. Instead of having to sprinkle the model-related logic all over the place, it’s easy to define the functionality as Mongoose models. Querying MongoDB for data also becomes quick and easy – and if you ever need some …
Sinon.js quick tip: How to stub/mock complex objects, such as DOM objects
Several of my readers have emailed me, asking about how to deal with more complex stubbing situations when using Sinon.js. In this article, we’ll look at how to stub objects which are deeply nested, and when functions have more complex return values and they interact with other objects. We’ll use DOM objects as a practical example, as they’re used quite …
What is a Humble Object and how does it help make your code testable?
Some of the most common testing-related questions I’m asked relate to testing real-world apps. There’s always a difficult part that’s hard to test. Most often, it’s been the database. Sometimes I’ve also been asked about HTTP-based APIs like RESTful APIs and such. Let’s imagine a typical situation where you have some code that uses a database. You’ve got some code …
Using Sinon.js to make unit testing real-life apps easy
One of the biggest stumbling blocks when writing unit tests is what to do when you have code that’s non-trivial. In real life projects, code often does all kinds of things that make testing hard. Ajax requests, timers, dates, accessing other browser features… or if you’re using Node.js, databases are always fun, and so is network or file access. All …
Sinon.js best practices for spies, stubs and mocks
Sinon is one of the most important tools for testing, as without it writing tests for more complex pieces of code such as Ajax, networking, databases, etc. would become difficult. In this article, I’ll show you the best practices for using Sinon, so you’ll be able to apply it to your own projects more easily. What are the differences between …
Unit testing Ajax requests with Mocha
Ajax requests can easily go wrong. You can’t guarantee the connection and the server always work correctly. They are also often used to send user input to the server and back, so it’s vital the data is handled correctly. But testing them can be tricky. It’s asynchronous, and also a good unit test must be isolated, so how can we …
How to unit test NodeJS HTTP requests?
I have a nodejs app where I wanted to unit test some HTTP requests. Node usually makes things simple, so I expected this to be like that too… but I soon discovered that this was not the case at all. When unit testing, you don’t want HTTP requests to go out and affect the result. Instead you create a fake …