the sweet smell of jasmine for testing javascript

18
The sweet smell of Jasmine when testing JavaScript Emma Armstrong @EmmaATester www.taooftesting.co.uk Emma.armstrong@towerswatson .com

Upload: emma-armstrong

Post on 30-Jul-2015

57 views

Category:

Software


0 download

TRANSCRIPT

Page 1: The sweet smell of jasmine for testing JavaScript

The sweet smell of Jasmine when testing JavaScript

Emma [email protected]@towerswatson.com

Page 2: The sweet smell of jasmine for testing JavaScript

Why test?

Page 3: The sweet smell of jasmine for testing JavaScript

How test?

ManualAutomated

Page 4: The sweet smell of jasmine for testing JavaScript

Using JavaScript

SupportAccessibility

Page 5: The sweet smell of jasmine for testing JavaScript

What is Jasmine?

Page 6: The sweet smell of jasmine for testing JavaScript

What is Behaviour Driven Development?

Page 7: The sweet smell of jasmine for testing JavaScript

What can you do with Jasmine?

SuitesSpecsMatchersSpies

Page 8: The sweet smell of jasmine for testing JavaScript

Jasmine Test

//Suitedescribe(’Spec name', function () { //Spec it(’what you are testing', function () {//arrange//act//assert expect(actualValue).matcher(); });});

Page 9: The sweet smell of jasmine for testing JavaScript

Matchers

toEqualtoThrowtoMatchtoContaintoBeTruthy/ tobeFalsytoBeLessThan/tobeGreaterThantoBeDefined/toBeUndefined

Page 10: The sweet smell of jasmine for testing JavaScript

SetUp and Teardown

beforeEach(function (){ //do what you need to;});

afterEach(function (){ //do what you need to;});

Page 11: The sweet smell of jasmine for testing JavaScript

SetUp and Teardown

beforeAll (function (){ //do what you need to;});

afterAll(function (){ //do what you need to;});

Page 12: The sweet smell of jasmine for testing JavaScript

Custom Matchers

beforeEach(function () { jasmine.addMatchers({

matcherName: function (util, customEqualityTesters)

return {Compare: function(actual, expected){ }}});});

Page 13: The sweet smell of jasmine for testing JavaScript

Spies

Spy on a methodspyOn(obj, ‘method’)

Verify its been calledExpect(obj.method).toHaveBeenCalled()

Verify its been calledExpect(obj.method).toHaveBeenCalledWith(‘x’,’y’)

Page 14: The sweet smell of jasmine for testing JavaScript

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()

Page 15: The sweet smell of jasmine for testing JavaScript

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";});

Page 16: The sweet smell of jasmine for testing JavaScript

Get Jasmine

You can get the latest version of Jasmine from:-

https://github.com/jasmine/jasmine/releases

Page 17: The sweet smell of jasmine for testing JavaScript

Example Code

https://github.com/EmmaATester/JasmineDemo

Page 18: The sweet smell of jasmine for testing JavaScript

The sweet smell of Jasmine when testing JavaScript

Emma [email protected]@towerswatson.com

Image: Jasmine by Ken Bosma