Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Testing Tuesday #17: How to Spy on JavaScript Methods with Jasmine

DZone's Guide to

Testing Tuesday #17: How to Spy on JavaScript Methods with Jasmine

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.



This is the 17th Testing Tuesday episode. Every week we share our insights and opinions on the software-testing space. Drop by every Tuesday to learn more! Last week we introduced testing JavaScript with Jasmine.

Spying on JavaScript Methods Using Jasmine

Jasmine is made for unit testing. Unit tests are supposed to test only one component of your application. A component can be a function, an object, a module, basically everything self-contained that acts like a black box to the outside world. You usually want to avoid your unit tests failing because another component failed. That’s why you want to test your components in isolation as much as possible.

For example, you may not want to send data to another server in your unit tests or you don’t want to manipulate a page’s DOM. But you want to make sure that the components that are responsible for these tasks get called correctly. How can we do that?

Jasmine provides a feature called spies. A spy listens to method calls on your objects and can be asked if and how a method got called later on.

In this screencast, we show you how you can use spies to check if methods got called. We check if data gets sent to the server without ever performing a request by spying on jQuery’s ajax method.

Up Next Testing Tuesday: Testing Asynchronous Operations

Next week we’ll show you how to test asynchronous JavaScript operations with Jasmine. If you’ve got any questions or suggestions, please leave us a comment!

Further Info


What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}