Posted on by & filed under Content - Highlights and Reviews, Programming & Development, Tutorials.

A guest post by Jonnie Spratley, who currently works for GE as a UI Developer on the Industrial Internet Team building user interfaces for next generation products, and teaches a variety of programming courses at AcademyX.

In this tip, we will be learning how to test AngularJS applications and code using Jasmine, which is a behavior-driven development framework for testing JavaScript code. Read Getting Set Up with Jasmine in JavaScript Testing with Jasmine to learn how to set up Jasmine. This test does not depend upon any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax, so that you can easily write your own tests.


The easiest way to get started writing Jasmine tests is to use Plnkr, which is an online community for creating, collaborating on and sharing your web development ideas:

  • Click on the Editor link to start editing a document
  • Click the New button and select AngularJS 1.0.x + Jasmine

How To

We are going to be creating a simple controller that will make an HTTP GET request, and also write a test for that controller. To fork this tutorial visit here.

Let’s start by creating a controller that will make an HTTP request. Here is the controller:

Let’s test this controller to be sure it is properly making the HTTP request with this test:

In the above code, we set an expectation with the expectGET method. When the test calls the flush method, flush will fail the test if the controller does not make all of the expected calls.

Now that we got our hands dirty writing a test, you can use these references for documentation on AngularJS and Jasmine testing API:

  • The Angular Scenario Runner simulates user interactions that will help you test your Angular application. You can find the basic scenario structure, and the API reference on the AngularJS E2E Testing page.
  • Jasmine is a behavior-driven testing framework for the JavaScript programming language. You can find the basic structure of a suite, the matchers reference and the reserved words on the Jasmine page.


Now that you’ve seen how easy it is to test in AngularJS, you can start writing tests for applications today. For more information see the Safari Books Online resources below.

Safari Books Online has the content you need

JavaScript Testing with Jasmine helps developers looking to keep their JavaScript code bug-free and who want to unit test using Jasmine, one of the most popular unit testing frameworks around. Any project of meaningful size should be automatically tested to help catch bugs as early as possible. Jasmine, a testing framework for JavaScript, makes it easy to test JavaScript projects, from browser-based applications to Node.js.
JavaScript Unit Testing will introduce and help you master the art of efficiently performing and automating JavaScript Unit tests for your web applications. Using the most popular JavaScript unit testing frameworks, you will develop, integrate, and automate all the tests you need to ensure the widest reach and success of your web application.
Developing an AngularJS Edge is intended for intermediate JavaScript programmers. The basic AngularJS concepts, components, and their applications are explained. You’ll build a working single-page weblog application using AngularJS, which will help you become proficient with using AngularJS to go out and create your own applications.
Develop smaller, lighter web apps that are simple to create and easy to test, extend, and maintain as they grow. AngularJS is a hands-on guide that introduces you to AngularJS, the open source JavaScript framework that uses Model–view–controller (MVC) architecture, data binding, client-side templates, and dependency injection to create a much-needed structure for building web apps.

About the author

jonnie Jonnie Spratley is currently working for GE as a UI Developer on the Industrial Internet Team building user interfaces for next generation products. He also teaches a variety of programming courses at AcademyX, and can be reached at @jonniespratley.

Tags: Angular Scenario Runner, AngularJS, Jasmine, Javascript, Plnkr, testing, unit tests,

Comments are closed.