Something about ‘JASMINE’
As you must have noticed in the previous post, we looked into a simple example of jasmine code, which navigates to a to-do list in AngularJS page.
So here, we’ll be talking more about jasmine
, mainly about jasmine syntax
The Structure
Jasmine
is an open-source testing framework for JavaScript
describe()
Everything in a jasmine
file starts in a describe
block. Consider this as the main section. Describe
block can have multipledescribe
blocks within itself, which are considered as subsections (nested blocks)
describe('Do something', function() {});
It
A describe
block consists of one or more it
blocks which act a separate test case. The definition of an it
block is same as a describe
block.
it('do something small', function() {});
Now let’s see some functions that are used in jasmine:
BeforeEach and AfterEach
There will always be something which you wanna perform every time before and after each it()
block. One way to achieve this is to call that particular code every time in your it()
block, but then isn’t this redundant? The easiest way of implementing this function is by using the same code inside beforeEach()
and afterEach()
function once
, an example would be something like this:
beforeEach(function(){
console.log("inside beforeEach block");
});afterEach(function(){
console.log("inside afterEach block");
});
Some matchers in jasmine….
Once you have created your test cases, you wanna do some validations
, which can be done by using expect()
statements that are available inside the jasmine framework.
toBe()
The defination of toBe()
is if both the values inside expect()
and toBe()
match with each other, then your test is passed, else failed.
expect(true).toBe(true);
not.toBe()
not.toBe()
is just the opposite of toBe()
.i.e if both the values inside expect()
and toBe()
don’t match with each other, then your test is passed, else failed.
it('validate 7 + 7 = 14', function() {
expected_test = element(by.id("some-id")).getText();
expected_test.then(function(text){
console.log("Result is : " + text);
expect(parseInt(text)).toBe(14)
});
});it('validate 7 + 7 != 10', function() {
expected_test = element(by.id("some-id")).getText();
expected_test.then(function(text){
console.log("Result is : " + text);
expect(parseInt(text)).not.toBe(10)
});
});
toEqual()
It checks whether the actual text is equal to the expected text.
describe('angularjs homepage todo list', function() {
browser.get('https://angularjs.org');
title = browser.getTitle();it('validate browser title', function() {
title.then(function(text){
expect(title).toEqual("AngularJS — Superheroic JavaScript MVW Framework");
});
});
});
toMatch()
This will match the complete or partial text
to be same as expected text.
describe('angularjs homepage todo list', function() {
browser.get('https://angularjs.org');
title = browser.getTitle();it('validate partial browser title', function() {
title.then(function(text){
expect(title).toMatch("AngularJS — Superheroic JavaScript");
});
});
});