23
Testing Backbone applications with Jasmine Leon van der Grient 2014

Testing Backbone applications with Jasmine

Embed Size (px)

DESCRIPTION

A (very) brief introduction to Jasmine and how to apply it to Backbone applications.

Citation preview

Page 1: Testing Backbone applications with Jasmine

Testing Backbone applications with Jasmine

Leon van der Grient 2014

Page 2: Testing Backbone applications with Jasmine

You know you should

Page 3: Testing Backbone applications with Jasmine

Why?

Confidence

Page 4: Testing Backbone applications with Jasmine

What?

Unitsbehavior

Page 5: Testing Backbone applications with Jasmine

When?

BeforeDuringAfter

Page 6: Testing Backbone applications with Jasmine

How?• qUnit

• SinonJS

• Mocha

• Jasmine

Page 7: Testing Backbone applications with Jasmine

Behavior Driven Testing

"Users should be able to like a message"

Page 8: Testing Backbone applications with Jasmine

Test suite | * Spec | * Expectations

Page 9: Testing Backbone applications with Jasmine

describe("Users should be able to", function () {

// Your specs

});

Page 10: Testing Backbone applications with Jasmine

describe("Users should be able to", function () {

it("like a message", function () {

var a = 1;

var b = 2;

var c = a + b;

expect(c).toEqual(3);

expect(c).not.toEqual(4);

});

});

Page 11: Testing Backbone applications with Jasmine

Spies and mocksspyOn($, "ajax");

$.ajax(arg);

!

expect($).toHaveBeenCalled();

expect($).toHaveBeenCalledWith(arg);

expect($.calls.count()).toEqual(1);

Page 12: Testing Backbone applications with Jasmine

But there is more!

spyOn($, "ajax").and.callFake(function () {

// mock ajax call

var response = {

success: true,

data: {...}

};

arguments[0].success(response);

});

Page 13: Testing Backbone applications with Jasmine
Page 14: Testing Backbone applications with Jasmine

Models

Collections

Views

Page 15: Testing Backbone applications with Jasmine

Testing models

it("like a message", function () {

this.message.set({liked:false});

this.message.like();

expect(this.message.get('liked').toBeTruthy();

});

Page 16: Testing Backbone applications with Jasmine

Testing views

it("like a message", function () {

this.message.set({liked:false});

this.view.likeMessage();

expect(this.message.get('liked').toBeTruthy();

});

Page 17: Testing Backbone applications with Jasmine

Using mocks

spyOn($, "ajax").and.callFake(function () {

// mock ajax call

var response = {

success: true,

data: {...}

};

arguments[0].success(response);

});

Page 18: Testing Backbone applications with Jasmine

Test for triggers

it("like a message", function () {

var spy = jasmine.createSpy();

this.view.listenTo(this.model, "change",

spy);

this.view.likeMessage();

expect(spy).toHaveBeenCalled();

});

Page 19: Testing Backbone applications with Jasmine

Test user interaction with jasmine-jquery plugin

it("follow a user", function () {

var followBtn = $('.follow-btn');

followBtn.click();

expect(followBtn).toHaveClass("unfollow");

});

Page 20: Testing Backbone applications with Jasmine

(too) many tests

toBeInDOM()

toBeVisible()

toHaveAttr()

toHaveClass()

toHaveText()

toHaveValue()

toBeChecked()

toBeDisabled()

Page 21: Testing Backbone applications with Jasmine

Results

Page 22: Testing Backbone applications with Jasmine
Page 23: Testing Backbone applications with Jasmine

Again, what?

• Methods

• The el property

• Events

• ...?