jasmine what:
Testing framework for javascript code. Very lightweight framework as it is not dependent on any other frameworks or any DOM. This follows BDD (business Driven Development), meaning based on user needs rather than code needs.
Jasmine concepts:
Add jasmine libraries, code to test and jasmine spec definition on code in html:
Code to be tested: main.js file with a very simple function
var sum = function(a, b){
Testing framework for javascript code. Very lightweight framework as it is not dependent on any other frameworks or any DOM. This follows BDD (business Driven Development), meaning based on user needs rather than code needs.
Jasmine concepts:
Suite –
A test suite begins with a call to the
global Jasmine function describe
with two parameters: a string and a function. String is the title of the suite.
Function is the block of code executes the suite.
points -
- defined using describe jasmine function
- describe is a namescope
- describe can have multiple describe in it
Specs –
Defined using describe jasmine function 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.
Points -
- it represents actual unit test
- it is also a namescope
- it can have multiple expectations (expect)
- A spec javascript which points to the module to be
tested. Naming convention of spec file is ‘
Spec.js’
Expectation –
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.
Points -
- expect – to check ‘it’ test passed or not
- systax – expect(
).toBe();
Matcher -
Each matcher implements a boolean comparison between
actual value and expected value. It reports to jasmine if expectation is true
or false. Jasmine will pass or fail the spec.
How to install/use jasmine:
Download link: https://github.com/jasmine/jasmine/releases
Version tested: 2.3.4
Option-01: using jasmine runner in web
Sample application - this application will test a very simple javascript function which simply adds two variables. testJasmine01.html is considered as jasmine spec runner where test result would be shown.
folder structure followed:
Add jasmine libraries, code to test and jasmine spec definition on code in html:
Code to be tested: main.js file with a very simple function
var sum = function(a, b){
return a * b;
}
Spec code: mainSpec.js
(function(){
describe("main file", function(){
describe("add method", function(){
it("Should add 2 numbers together", function(){
expect(sum(2,3)).toBe(5);
});
});
});
})();
Run the test: just open the html page in a browser. Output should look like below where the test failed.
Fix the error: change main.js, * to +
Run the test again: this time it passes
Modify the spec to add concatenation test:
(function(){
describe("main file", function(){
describe("add method", function(){
it("Should add 2 numbers together", function(){
expect(sum(2,3)).toBe(5);
});
it("Should concatenate two values", function(){
expect(sum('Riaz ', 'Ahmed')).toBe('Riaz Ahmed');
});
});
});
})();
Run the test:
Option-02: using command line jasmine.
This will be covered in a separate post in future.
Resource:
Fix the error: change main.js, * to +
var sum = function(a, b){
return a + b;
}
Run the test again: this time it passes
Modify the spec to add concatenation test:
(function(){
describe("main file", function(){
describe("add method", function(){
it("Should add 2 numbers together", function(){
expect(sum(2,3)).toBe(5);
});
it("Should concatenate two values", function(){
expect(sum('Riaz ', 'Ahmed')).toBe('Riaz Ahmed');
});
});
});
})();
Run the test:
Option-02: using command line jasmine.
This will be covered in a separate post in future.
Resource:
- http://jasmine.github.io/2.0/introduction.html
No comments:
Post a Comment