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

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.



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


Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}