With reference to the filter created in a previous article, now we want to create a test.

The test can be used both to check the proper filter working and as a function specification document.

In this case we want to check the following conditions:

- the application of rot13 on a given string gives the expected result

- the application of rot13 on an encrypted string gives back the initial result

- the application of rot13 on invalid strings gives an empty string

A possible test suite for Jasmine is the following

describe("MyApp Filters", function() {
  beforeEach(module("MyApp"));

  describe("rot13", function() {
     it("should cipher string values with ro13",
        inject(function(rot13Filter) {
       expect(rot13Filter("foo")).toBe("sbb");
       expect(rot13Filter("sbb")).toBe("foo");
       expect(rot13Filter("")).toBe("");
       expect(rot13Filter(false)).toBe("");
       expect(rot13Filter(true)).toBe("");
       expect(rot13Filter([1,1,1])).toBe("");
     }));
   });
});

beforeEach(module('MyApp')) needs to be called before executing any test. Such call loads the rot13Filter to the injector, so it can be used during test run.

Afterward, Jasmine assertions may be used to verify the expected results on each possible input. 

A good starting point is angular-seed (https://github.com/angular/angular-seed), which is already equipped with a basic configuration for Karma and Jasmine, tools used by the development team of AngularJS.

Antonello Pasella

Freelance Frontent Developer, PHP Expert , Website , Git home page , @apasella , Linkedin profile
15+ year experience in web programming, fucused on cross platform frontend and game development (even remote). Web enthusiast and game passionate, full stack programmer ready to dive into new projects. Top skills: AngularJS Phonegap PHP

All articles by Antonello Pasella

Comments

comments powered by Disqus

cloudparty

Follow Us