57
Jasmine / Sinon.js Jorge Falcão Sergio Azevedo [email protected] @sergioazevedo Saturday, May 14, 2011

Jasmine - Sinon - BrazilJS

Embed Size (px)

DESCRIPTION

Lightning talk realizado no BrazilJS

Citation preview

Page 1: Jasmine - Sinon - BrazilJS

Jasmine / Sinon.js

Jorge Falcão Sergio Azevedo

[email protected]@sergioazevedo

Saturday, May 14, 2011

Page 2: Jasmine - Sinon - BrazilJS

Jasmine

Saturday, May 14, 2011

Page 3: Jasmine - Sinon - BrazilJS

Jasmine

Saturday, May 14, 2011

Page 4: Jasmine - Sinon - BrazilJS

Jasmine

Saturday, May 14, 2011

Page 5: Jasmine - Sinon - BrazilJS

Jasmine

Saturday, May 14, 2011

Page 6: Jasmine - Sinon - BrazilJS

Jasmine

Saturday, May 14, 2011

Page 7: Jasmine - Sinon - BrazilJS

//# ContaSpec.js #

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); });

Jasmine / Spec

Saturday, May 14, 2011

Page 8: Jasmine - Sinon - BrazilJS

//# ContaSpec.js #

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); });

Jasmine / Spec

Suite

Saturday, May 14, 2011

Page 9: Jasmine - Sinon - BrazilJS

//# ContaSpec.js #

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); });

Jasmine / Spec

Saturday, May 14, 2011

Page 10: Jasmine - Sinon - BrazilJS

//# ContaSpec.js #

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); });

Jasmine / Spec

Spec

Saturday, May 14, 2011

Page 11: Jasmine - Sinon - BrazilJS

//# ContaSpec.js #

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); });

Jasmine / Spec

Saturday, May 14, 2011

Page 12: Jasmine - Sinon - BrazilJS

//# ContaSpec.js #

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); });

Jasmine / Spec

Expectation

Saturday, May 14, 2011

Page 13: Jasmine - Sinon - BrazilJS

//# ContaSpec.js #

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); });

Jasmine / Spec

Saturday, May 14, 2011

Page 14: Jasmine - Sinon - BrazilJS

//# ContaSpec.js #

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); });

Jasmine / Spec

Matcher

Saturday, May 14, 2011

Page 15: Jasmine - Sinon - BrazilJS

//# ContaSpec.js #

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); });

Jasmine / Spec

Saturday, May 14, 2011

Page 16: Jasmine - Sinon - BrazilJS

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300); expect(conta.saldo).toEqual(700); }); it("deve permitir depositos",function(){ var conta = new Conta({saldo:1000}); conta.deposita(200); expect(conta.saldo).toEqual(1200); }); });

Jasmine / Spec

Saturday, May 14, 2011

Page 17: Jasmine - Sinon - BrazilJS

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300); expect(conta.saldo).toEqual(700); }); it("deve permitir depositos",function(){ var conta = new Conta({saldo:1000}); conta.deposita(200); expect(conta.saldo).toEqual(1200); }); });

Jasmine / Spec

Saturday, May 14, 2011

Page 18: Jasmine - Sinon - BrazilJS

describe('Conta', function(){

it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300); expect(conta.saldo).toEqual(700); }); it("deve permitir depositos",function(){ var conta = new Conta({saldo:1000}); conta.deposita(200); expect(conta.saldo).toEqual(1200); }); });

Jasmine / beforeEach

Saturday, May 14, 2011

Page 19: Jasmine - Sinon - BrazilJS

describe('Conta', function(){var conta;beforeEach(function() { var conta = new Conta({saldo:1000}); });

it("deve realizar saques", function(){ conta.saca(300); expect(conta.saldo).toEqual(700); }); it("deve permitir depositos",function(){ conta.deposita(200); expect(conta.saldo).toEqual(1200); }); });

Jasmine / beforeEach

Saturday, May 14, 2011

Page 20: Jasmine - Sinon - BrazilJS

describe('Conta', function(){var conta;beforeEach(function() { var conta = new Conta({saldo:1000}); });

it("deve realizar saques", function(){ conta.saca(300); expect(conta.saldo).toEqual(700); }); it("deve permitir depositos",function(){ conta.deposita(200); expect(conta.saldo).toEqual(1200); }); });

Jasmine / beforeEach

afterE

ach()

too!

Saturday, May 14, 2011

Page 21: Jasmine - Sinon - BrazilJS

describe('Conta', function(){ var conta; describe('Conta Sem Bloqueio',function(){ beforeEach(function() { conta = new Conta({saldo:1000}); }); it("deve realizar saques", function(){

... }); });

describe('Conta Bloqueada',function(){

beforeEach(function() { conta = new Conta({saldo:1000}); conta.bloquear(); }); it("nao deve realizar saques", function(){

... }); });});

Jasmine / Nested Spec

Saturday, May 14, 2011

Page 22: Jasmine - Sinon - BrazilJS

describe('Conta', function(){ var conta; describe('Conta Sem Bloqueio',function(){ beforeEach(function() { conta = new Conta({saldo:1000}); }); it("deve realizar saques", function(){

... }); });

describe('Conta Bloqueada',function(){

beforeEach(function() { conta = new Conta({saldo:1000}); conta.bloquear(); }); it("nao deve realizar saques", function(){

... }); });});

Jasmine / Nested Spec

Saturday, May 14, 2011

Page 23: Jasmine - Sinon - BrazilJS

expect(conta.saldo).toEqual(1000);expect(conta).toBe(conta);expect(conta.titular).toMatch(/ana/);expect(conta.saldo).toBeDefined();expect(conta.dataEncerramento).toBeNull();expect(conta.saca(-500).toThrow(Error);

expect(conta.saldo).not.toEqual(40);expect(conta).not.toBe(“Mario”);...

Jasmine / Matchers

É possível negar um Matcher.

Saturday, May 14, 2011

Page 24: Jasmine - Sinon - BrazilJS

expect(conta.saldo).toEqual(1000);expect(conta).toBe(conta);expect(conta.titular).toMatch(/ana/);expect(conta.saldo).toBeDefined();expect(conta.dataEncerramento).toBeNull();expect(conta.saca(-500).toThrow(Error);

expect(conta.saldo).not.toEqual(40);expect(conta).not.toBe(“Mario”);...

Jasmine / Matchers

É possível negar um Matcher.

Também é possível

criar seu próprio Matcher

Saturday, May 14, 2011

Page 25: Jasmine - Sinon - BrazilJS

describe('Banco',function(){ it("deve taxar suas contas", function(){ var banco = new Banco(); var conta = new Conta(); banco.addConta(conta); spyOn(conta,'descontarTarifa');

banco.taxarContas(); expect(conta.descontarTarifa).toHaveBeenCalled(); }); });

Jasmine / Spies

Saturday, May 14, 2011

Page 26: Jasmine - Sinon - BrazilJS

describe('Banco',function(){ it("deve taxar suas contas", function(){ var banco = new Banco(); var conta = new Conta(); banco.addConta(conta); spyOn(conta,'descontarTarifa');

banco.taxarContas(); expect(conta.descontarTarifa).toHaveBeenCalled(); }); });

Jasmine / Spies

Saturday, May 14, 2011

Page 27: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){

it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300);

expect(a).toEqual(2); });

});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 28: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){

it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300);

expect(a).toEqual(2); });

});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 29: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){

it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300);

expect(a).toEqual(2); });

});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 30: Jasmine - Sinon - BrazilJS

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 31: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){

it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300);

expect(a).toEqual(2); });

});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 32: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){

it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300);

expect(a).toEqual(2); });

});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 33: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){

it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300);

expect(a).toEqual(2); });

});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 34: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){

it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300);

waits(300); runs(function() { expect(a).toEqual(2); }); });

});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 35: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){

it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300);

waits(300); runs(function() { expect(a).toEqual(2); }); });

});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 36: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){

it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300);

waits(300); runs(function() { expect(a).toEqual(2); }); });

});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 37: Jasmine - Sinon - BrazilJS

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 38: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){ it("exemplo com waitsFor", function() { var a = 1; var somado = false; setTimeout(function() { a++; console.debug("somando"); somado = true; }, 1000);

waitsFor(function() { return somado; }, "aguardando soma", 4000);

runs(function() { expect(a).toEqual(2); }); });});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 39: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){ it("exemplo com waitsFor", function() { var a = 1; var somado = false; setTimeout(function() { a++; console.debug("somando"); somado = true; }, 1000);

waitsFor(function() { return somado; }, "aguardando soma", 4000);

runs(function() { expect(a).toEqual(2); }); });});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 40: Jasmine - Sinon - BrazilJS

describe("Testes Assicronos", function(){ it("exemplo com waitsFor", function() { var a = 1; var somado = false; setTimeout(function() { a++; console.debug("somando"); somado = true; }, 1000);

waitsFor(function() { return somado; }, "aguardando soma", 4000);

runs(function() { expect(a).toEqual(2); }); });});

Jasmine / Asynchronous specs

Saturday, May 14, 2011

Page 41: Jasmine - Sinon - BrazilJS

Sinon.JS

Saturday, May 14, 2011

Page 42: Jasmine - Sinon - BrazilJS

Sinon.js

• Spies• Stubs• Mocks• Fake Timers• Fake XHR• Fake Server

Saturday, May 14, 2011

Page 43: Jasmine - Sinon - BrazilJS

Sinon.js

• Spies• Stubs• Mocks• Fake Timers• Fake XHR• Fake Server

Saturday, May 14, 2011

Page 44: Jasmine - Sinon - BrazilJS

describe("Sinon Examples", function(){

it('FakeTimers', function(){ var data = new Date(); expect(elapsedTime(myDate)).toEqual(2); });

});

Sinon.js/Fake Timers

Saturday, May 14, 2011

Page 45: Jasmine - Sinon - BrazilJS

describe("Sinon Examples", function(){

it('FakeTimers', function(){ var data = new Date(); expect(elapsedTime(myDate)).toEqual(2); });

});

Sinon.js/Fake Timers

Depende da v

elocidade

do browser!

Saturday, May 14, 2011

Page 46: Jasmine - Sinon - BrazilJS

describe("Sinon Examples", function(){

it('FakeTimers', function(){ var clock = sinon.UseFakeTimers();

var data = new Date();

clock.tick(2000);

expect(elapsedTime(myDate)).toEqual(2);

clock.restore();

});

});

Sinon.js/Fake Timers

Saturday, May 14, 2011

Page 47: Jasmine - Sinon - BrazilJS

describe("Sinon Examples", function(){

it('FakeTimers', function(){ var clock = sinon.UseFakeTimers();

var data = new Date();

clock.tick(2000);

expect(elapsedTime(myDate)).toEqual(2);

clock.restore();

});

});

Sinon.js/Fake Timers

Saturday, May 14, 2011

Page 48: Jasmine - Sinon - BrazilJS

describe("Sinon Examples", function(){

it('FakeTimers', function(){ var clock = sinon.UseFakeTimers();

var data = new Date();

clock.tick(2000);

expect(elapsedTime(myDate)).toEqual(2);

clock.restore();

});

});

Sinon.js/Fake Timers

Saturday, May 14, 2011

Page 49: Jasmine - Sinon - BrazilJS

describe("Sinon Examples", function(){

it('FakeTimers', function(){ var clock = sinon.UseFakeTimers();

var data = new Date();

clock.tick(2000);

expect(elapsedTime(myDate)).toEqual(2);

clock.restore();

});

});

Sinon.js/Fake Timers

Saturday, May 14, 2011

Page 50: Jasmine - Sinon - BrazilJS

describe("Sinon Examples", function(){

it('FakeTimers', function(){ var clock = sinon.UseFakeTimers();

var data = new Date();

clock.tick(2000);

expect(elapsedTime(myDate)).toEqual(2);

clock.restore();

});

});

Sinon.js/Fake Timers

beforeEach e

afterEach

podem ser ut

eis.

Saturday, May 14, 2011

Page 51: Jasmine - Sinon - BrazilJS

describe('Sinon',function(){ var server; beforeEach(function() { var server = sinon.useFakeServer(); }); afterEach(function() { server.restore(); }); it("FakeServer example", function(){ server.respondWith('GET', '/users.json', [200, {"Content-Type" : "application/json"}, '[{id:1, login: "Jason"}]']); var callback = sinon.spy(); $.ajax({ url: '/users.json', success: callback }); assert( callback.calledWith([{id: 1, login: "Json"}]); ); });});

Sinon.js/Fake Server

Saturday, May 14, 2011

Page 52: Jasmine - Sinon - BrazilJS

describe('Sinon',function(){ var server; beforeEach(function() { var server = sinon.useFakeServer(); }); afterEach(function() { server.restore(); }); it("FakeServer example", function(){ server.respondWith('GET', '/users.json', [200, {"Content-Type" : "application/json"}, '[{id:1, login: "Jason"}]']); var callback = sinon.spy(); $.ajax({ url: '/users.json', success: callback }); assert( callback.calledWith([{id: 1, login: "Json"}]); ); });});

Sinon.js/Fake Server

Saturday, May 14, 2011

Page 53: Jasmine - Sinon - BrazilJS

describe('Sinon',function(){ var server; beforeEach(function() { var server = sinon.useFakeServer(); }); afterEach(function() { server.restore(); }); it("FakeServer example", function(){ server.respondWith('GET', '/users.json', [200, {"Content-Type" : "application/json"}, '[{id:1, login: "Jason"}]']); var callback = sinon.spy(); $.ajax({ url: '/users.json', success: callback }); assert( callback.calledWith([{id: 1, login: "Json"}]); ); });});

Sinon.js/Fake Server

Saturday, May 14, 2011

Page 54: Jasmine - Sinon - BrazilJS

describe('Sinon',function(){ var server; beforeEach(function() { var server = sinon.useFakeServer(); }); afterEach(function() { server.restore(); }); it("FakeServer example", function(){ server.respondWith('GET', '/users.json', [200, {"Content-Type" : "application/json"}, '[{id:1, login: "Jason"}]']); var callback = sinon.spy(); $.ajax({ url: '/users.json', success: callback }); assert( callback.calledWith([{id: 1, login: "Json"}]); ); });});

Sinon.js/Fake Server

Saturday, May 14, 2011

Page 55: Jasmine - Sinon - BrazilJS

describe('Sinon',function(){ var server; beforeEach(function() { var server = sinon.useFakeServer(); }); afterEach(function() { server.restore(); }); it("FakeServer example", function(){ server.respondWith('GET', '/users.json', [200, {"Content-Type" : "application/json"}, '[{id:1, login: "Jason"}]']); var callback = sinon.spy(); $.ajax({ url: '/users.json', success: callback }); assert( callback.calledWith([{id: 1, login: "Json"}]); ); });});

Sinon.js/Fake Server

Saturday, May 14, 2011

Page 56: Jasmine - Sinon - BrazilJS

Referências

• JasmineBDD• http://pivotal.github.com/jasmine/

• Sinon.js• http://sinonjs.org/

Saturday, May 14, 2011