Book Image

CoffeeScript Application Development Cookbook

By : Mike Hatfield
Book Image

CoffeeScript Application Development Cookbook

By: Mike Hatfield

Overview of this book

Table of Contents (18 chapters)
CoffeeScript Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Stubbing and mocking with Sinon


In this recipe, we will use a JavaScript library called Sinon to create test doubles to help improve our tests.

Test doubles are fake objects that replace actual dependencies in our tests. Why would you want to use a mocking library? Some of the benefits of using a library such as Sinon include the following:

  • They allow us to focus on our code under test without having to worry that its dependencies are working correctly, or even exist

  • They allow us to speed up our tests by faking out network calls, database access, and other tasks that slow our tests down

Getting ready

In this section, we create and use stubs in our tests.

A stub is a fake object that provides stand-in functionality of an external dependency of our object under test.

For our example, we want to test an Employee Manager class that is responsible for managing collections of employees. Employee Manager uses a data service that retrieves employee information from an API.

Since our tests are concerned...