the sweet smell of jasmine for testing javascript
TRANSCRIPT
The sweet smell of Jasmine when testing JavaScript
Emma [email protected]@towerswatson.com
Why test?
How test?
ManualAutomated
Using JavaScript
SupportAccessibility
What is Jasmine?
What is Behaviour Driven Development?
What can you do with Jasmine?
SuitesSpecsMatchersSpies
Jasmine Test
//Suitedescribe(’Spec name', function () { //Spec it(’what you are testing', function () {//arrange//act//assert expect(actualValue).matcher(); });});
Matchers
toEqualtoThrowtoMatchtoContaintoBeTruthy/ tobeFalsytoBeLessThan/tobeGreaterThantoBeDefined/toBeUndefined
SetUp and Teardown
beforeEach(function (){ //do what you need to;});
afterEach(function (){ //do what you need to;});
SetUp and Teardown
beforeAll (function (){ //do what you need to;});
afterAll(function (){ //do what you need to;});
Custom Matchers
beforeEach(function () { jasmine.addMatchers({
matcherName: function (util, customEqualityTesters)
return {Compare: function(actual, expected){ }}});});
Spies
Spy on a methodspyOn(obj, ‘method’)
Verify its been calledExpect(obj.method).toHaveBeenCalled()
Verify its been calledExpect(obj.method).toHaveBeenCalledWith(‘x’,’y’)
Spies continued……Fix the return value of the spyspyOn(obj, ‘method’).andReturn(‘Return me’)
How many times has it been called?Obj.method.callcount
What were the arguments to the last callObj.mthod.mostrecentCall.args
Reset calls made to the spy so farObject.method.reset()
Spies continued……Obj.method =Jasmine.createSpy(“spy Name”);
jasmine.createSpy("spy Name ").andReturn(“Return Value");
jasmine.createSpy(" spy Name ").andCallFake(function() { do something; return “Return Value";});
Get Jasmine
You can get the latest version of Jasmine from:-
https://github.com/jasmine/jasmine/releases
Example Code
https://github.com/EmmaATester/JasmineDemo
The sweet smell of Jasmine when testing JavaScript
Emma [email protected]@towerswatson.com
Image: Jasmine by Ken Bosma