Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testes no Rails: Além do Ruby

Testes no Rails: Além do Ruby

Um ponto de vista sobre como cuidar e evoluir de forma sustentável a camada de JavaScripts de uma aplicação Rails. Utilizando como base uma abordagem TDD, indo além dos simples testes de aceitação: escrevendo testes de unidade e desenvolvendo código fácil de ser testado.

Halan Pinheiro

September 03, 2013
Tweet

Other Decks in Programming

Transcript

  1. ‣Como vamos poder comer? ‣ Por que comemos? ‣ Onde

    vamos almoçar? terça-feira, 3 de setembro de 13
  2. ‣Como vamos poder comer? ‣ Por que comemos? ‣ Onde

    vamos almoçar? Sobrevivência Interrogação Sofisticação terça-feira, 3 de setembro de 13
  3. ‣Como? ‣Por quê? ‣Onde? ... Embora uma grande civilização leve

    milênios para passar pelas fases do Como, do Porquê e do Onde, pequenos agrupamentos sociais podem passar por elas com extrema rapidez. terça-feira, 3 de setembro de 13
  4. Mocha <html> <head> <title>Mocha Tests</title> <script type="text/javascript" src="mocha.js"></script> <script> mocha.setup('bdd');

    var assert = function(expression){ if(!expression) throw; }; describe('Winston Smith sum', function(){ it('2 + 2 always be equal 4', function(){ assert(2 + 2 == 4); }); }); mocha.run(); </script> </head> <body></body> </html> terça-feira, 3 de setembro de 13
  5. Mocha <html> <head> <title>Mocha Tests</title> <script type="text/javascript" src="mocha.js"></script> <script> mocha.setup('bdd');

    var assert = function(expression){ if(!expression) throw; }; describe('Winston Smith sum', function(){ it('2 + 2 always be equal 4', function(){ assert(2 + 2 == 4); }); }); mocha.run(); </script> </head> <body></body> </html> terça-feira, 3 de setembro de 13
  6. Mocha <html> <head> <title>Mocha Tests</title> <script type="text/javascript" src="mocha.js"></script> <script> mocha.setup('bdd');

    var assert = function(expression){ if(!expression) throw; }; describe('Winston Smith sum', function(){ it('2 + 2 always be equal 4', function(){ assert(2 + 2 == 4); }); }); mocha.run(); </script> </head> <body></body> </html> terça-feira, 3 de setembro de 13
  7. Mocha <html> <head> <title>Mocha Tests</title> <script type="text/javascript" src="mocha.js"></script> <script> mocha.setup('bdd');

    var assert = function(expression){ if(!expression) throw; }; describe('Winston Smith sum', function(){ it('2 + 2 always be equal 4', function(){ assert(2 + 2 == 4); }); }); mocha.run(); </script> </head> <body></body> </html> terça-feira, 3 de setembro de 13
  8. Mocha <html> <head> <title>Mocha Tests</title> <script type="text/javascript" src="mocha.js"></script> <script> mocha.setup('bdd');

    var assert = function(expression){ if(!expression) throw; }; describe('Winston Smith sum', function(){ it('2 + 2 always be equal 4', function(){ assert(2 + 2 == 4); }); }); mocha.run(); </script> </head> <body></body> </html> terça-feira, 3 de setembro de 13
  9. Mocha + Chai.js window.expect = chai.expect; describe('Winston Smith sum', function(){

    it('2+2 always be equal 4', function(){ expect( 2 + 2 ).to.be.equal(4); }); }); describe( 'we love the expressiveness of chai: write.with.dots.is.cool', function(){ it('2+2 always be equal 4 and 4 is a number', function(){ expect(2 + 2).to.be.equal(4).and.is.a('number'); }); }); terça-feira, 3 de setembro de 13
  10. Mocha + Chai.js window.expect = chai.expect; describe('Winston Smith sum', function(){

    it('2+2 always be equal 4', function(){ expect( 2 + 2 ).to.be.equal(4); }); }); describe( 'we love the expressiveness of chai: write.with.dots.is.cool', function(){ it('2+2 always be equal 4 and 4 is a number', function(){ expect(2 + 2).to.be.equal(4).and.is.a('number'); }); }); terça-feira, 3 de setembro de 13
  11. Mocha + Chai.js window.expect = chai.expect; describe('Winston Smith sum', function(){

    it('2+2 always be equal 4', function(){ expect( 2 + 2 ).to.be.equal(4); }); }); describe( 'we love the expressiveness of chai: write.with.dots.is.cool', function(){ it('2+2 always be equal 4 and 4 is a number', function(){ expect(2 + 2).to.be.equal(4).and.is.a('number'); }); }); terça-feira, 3 de setembro de 13
  12. Mocha + Chai.js window.expect = chai.expect; describe('Winston Smith sum', function(){

    it('2+2 always be equal 4', function(){ expect( 2 + 2 ).to.be.equal(4); }); }); describe( 'we love the expressiveness of chai: write.with.dots.is.cool', function(){ it('2+2 always be equal 4 and 4 is a number', function(){ expect(2 + 2).to.be.equal(4).and.is.a('number'); }); }); terça-feira, 3 de setembro de 13
  13. Sinon.js //= require products describe('Products', function(){ beforeEach(function(){ this.server = sinon.fakeServer.create();

    this.server.requests[0].respond( 200, { "Content-Type": "application/json" }, JSON.stringify([{id: 2}, {id: 7}, {id: 8}]); ); }); afterEach(function(){ this.restore(); }); it('fetch loads some products', function(){ var new products = new Products() products.fetch(); expect(products).to.be.length(6); }); }); terça-feira, 3 de setembro de 13
  14. Sinon.js //= require products describe('Products', function(){ beforeEach(function(){ this.server = sinon.fakeServer.create();

    this.server.requests[0].respond( 200, { "Content-Type": "application/json" }, JSON.stringify([{id: 2}, {id: 7}, {id: 8}]); ); }); afterEach(function(){ this.restore(); }); it('fetch loads some products', function(){ var new products = new Products() products.fetch(); expect(products).to.be.length(6); }); }); terça-feira, 3 de setembro de 13
  15. Sinon.js //= require products describe('Products', function(){ beforeEach(function(){ this.server = sinon.fakeServer.create();

    this.server.requests[0].respond( 200, { "Content-Type": "application/json" }, JSON.stringify([{id: 2}, {id: 7}, {id: 8}]); ); }); afterEach(function(){ this.restore(); }); it('fetch loads some products', function(){ var new products = new Products() products.fetch(); expect(products).to.be.length(6); }); }); terça-feira, 3 de setembro de 13
  16. Sinon.js //= require products describe('Products', function(){ beforeEach(function(){ this.server = sinon.fakeServer.create();

    this.server.requests[0].respond( 200, { "Content-Type": "application/json" }, JSON.stringify([{id: 2}, {id: 7}, {id: 8}]); ); }); afterEach(function(){ this.restore(); }); it('fetch loads some products', function(){ var new products = new Products() products.fetch(); expect(products).to.be.length(3); }); }); terça-feira, 3 de setembro de 13
  17. Sinon.js //= require products describe('Products', function(){ beforeEach(function(){ this.server = sinon.fakeServer.create();

    this.server.requests[0].respond( 200, { "Content-Type": "application/json" }, JSON.stringify([{id: 2}, {id: 7}, {id: 8}]); ); }); afterEach(function(){ this.restore(); }); it('fetch loads some products', function(){ var new products = new Products() products.fetch(); expect(products).to.be.length(3); }); }); terça-feira, 3 de setembro de 13
  18. Sinon.js //= require products describe('Products', function(){ beforeEach(function(){ this.server = sinon.fakeServer.create();

    this.server.requests[0].respond( 200, { "Content-Type": "application/json" }, JSON.stringify([{id: 2}, {id: 7}, {id: 8}]); ); }); afterEach(function(){ this.restore(); }); it('fetch loads some products', function(){ var new products = new Products() products.fetch(); expect(products).to.be.length(3); }); }); terça-feira, 3 de setembro de 13
  19. $ bundle exec rake konacha:run ................... Finished in 0.503 seconds

    19 examples, 0 failed, 0 pending terça-feira, 3 de setembro de 13
  20. $ bundle exec rake konacha:run ................... Finished in 0.503 seconds

    19 examples, 0 failed, 0 pending terça-feira, 3 de setembro de 13