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 to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software



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


Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Clemens Helm, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}