Testing is a very important activity in the software development. This type of development particularly became important when the value of the software has begun to grow.
As the customers’ satisfaction is the most important, and the large amounts of money is invested in the area of the software development, each part of application should be thoroughly tested and all errors must be corrected as soon as possible.
This is the reason why testing has become equally important as the software development and a lot of companies are increasingly investing in this area.
What is Jasmine.js?
Jasmine is an automated, open testing source framework for JavaScript.
As more and more parts of our applications are written in JavaScript, it’s important to have them covered with automated tests and this is the reason we use Jasmine.js.
Why Jasmine.js?
There are many tools for writing unit tests, but we have decided to use Jasmine.js because in the some projects we use Angular.js, which is compatible with Jasmine.js and also because it is very easy to use.
Jasmine has a clean, obvious syntax, so you can easily write tests and also Jasmine.js aims to be easy to read.
Download & Install
I'm going to use 2.1.3 version, at time of writing this is the latest version of Jasmine.js. The download page is here: jasmine 2.1.3.
Running
The file SpecRunner.html is used to running Jasmine.js. Opening SpecRunner.html will run the included specs.
General
A test suite begins with a call to the global Jasmine function describe with two parameters: a string and a function. The string is the title or name for a spec and usually this is what is being tested. The function is a block of code that implements the suite.
Specs are defined by calling the global Jasmine function it, which, like describe, takes a string and a function. The string is the title of the spec and the function is the spec, or test. A spec contains one or more expectations that test the state of the code. An expectation in Jasmine is an assertion that is either true or false. A spec with all true expectations is a passing spec. A spec with one or more false expectations is a failing spec.
Expectations are built with the function expect which takes a value, called the actual. It is chained with a Matcher function, which takes the expected value.
Example
Basic example and some included matcher you can find below:
describe("Matchers included:", function() { it("The 'toBe' matcher", function() { var a = 12; var b = a; expect(a).toBe(b); expect(a).not.toBe(null); }); describe("The 'toEqual' matcher", function () { it("works for simple variables", function () { var a = 12; expect(a).toEqual(12); }); it("should work for objects", function () { var object1 = { a: 12, b: 34 }; var object2 = { a: 12, b: 34 }; expect(object1).toEqual(object2); }); }); it("The 'toMatch' matcher", function () { var message = "one two three"; expect(message).toMatch("two"); expect(message).not.toMatch(/four/); }); it("The 'toBeDefined', 'toBeUndefined' and 'toBeNull' matchers", function () { var a = { value: "test" }; var b = null; expect(a.value).toBeDefined(); expect(a.key).toBeUndefined(); expect(null).toBeNull(); expect(b).toBeNull(); expect(a).not.toBeNull(); }); it("The 'toBeTruthy' matcher", function () { var a; var b = "test"; expect(b).toBeTruthy(); expect(a).not.toBeTruthy(); }); it("The 'toBeFalsy' matcher", function () { var a; var b = "test"; expect(a).toBeFalsy(); expect(b).not.toBeFalsy(); }); it("The 'toContain' matcher", function () { var a = ["one", "two", "three"]; expect(a).toContain("one"); expect(a).not.toContain("four"); }); it("The 'toBeLessThan' matcher", function () { var pi = 3.14, e = 2.78; expect(e).toBeLessThan(pi); expect(pi).not.toBeLessThan(e); }); it("The 'toBeGreaterThan' matcher", function () { var pi = 3.14, e = 2.78; expect(pi).toBeGreaterThan(e); expect(e).not.toBeGreaterThan(pi); }); }); |
Happy coding!
The post Testing using Jasmine.js appeared first on Merit Solutions.