SlideShare uma empresa Scribd logo
1 de 57
Baixar para ler offline
Jasmine / Sinon.js

                                  Jorge Falcão
                                 Sergio Azevedo
                         sergio.azevedo@intelie.com.br
                                @sergioazevedo
Saturday, May 14, 2011
Jasmine



Saturday, May 14, 2011
Jasmine




Saturday, May 14, 2011
Jasmine




Saturday, May 14, 2011
Jasmine




Saturday, May 14, 2011
Jasmine




Saturday, May 14, 2011
Jasmine / Spec
     //# ContaSpec.js #

     describe('Conta', function(){

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

     });




Saturday, May 14, 2011
Jasmine / Spec
     //# ContaSpec.js #

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

     });




Saturday, May 14, 2011
Jasmine / Spec
     //# ContaSpec.js #

     describe('Conta', function(){

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

     });




Saturday, May 14, 2011
Jasmine / Spec
     //# ContaSpec.js #

     describe('Conta', function(){

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

     });




Saturday, May 14, 2011
Jasmine / Spec
     //# ContaSpec.js #

     describe('Conta', function(){

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

     });




Saturday, May 14, 2011
Jasmine / Spec
     //# ContaSpec.js #

     describe('Conta', function(){

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

     });




Saturday, May 14, 2011
Jasmine / Spec
     //# ContaSpec.js #

     describe('Conta', function(){

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

     });




Saturday, May 14, 2011
Jasmine / Spec
     //# ContaSpec.js #

     describe('Conta', function(){

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




Saturday, May 14, 2011
Jasmine / Spec
     //# ContaSpec.js #

     describe('Conta', function(){

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

     });




Saturday, May 14, 2011
Jasmine / Spec
     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);
           });

     });




Saturday, May 14, 2011
Jasmine / Spec
     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);
           });

     });




Saturday, May 14, 2011
Jasmine / beforeEach
     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);
           });

     });




Saturday, May 14, 2011
Jasmine / beforeEach
     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);
           });

     });


Saturday, May 14, 2011
Jasmine / beforeEach
     describe('Conta', function(){
      var conta;
      beforeEach(function() {
          var conta = new Conta({saldo:1000});          h()
                                                    Eac
        });                                      ter o!
                                               af to
        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);
           });

     });


Saturday, May 14, 2011
Jasmine / Nested Spec
     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(){
           ...
         });
       });
     });
Saturday, May 14, 2011
Jasmine / Nested Spec
     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(){
           ...
         });
       });
     });
Saturday, May 14, 2011
Jasmine / Matchers

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


              É possíve l negar um Matcher.


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




Saturday, May 14, 2011
Jasmine / Matchers
                                        Tamb
   expect(conta.saldo).toEqual(1000);  cria ém é po
                                           r se      ssív
   expect(conta).toBe(conta);                   u pr      el
                                            Matc     ópri
   expect(conta.titular).toMatch(/ana/);         her      o
   expect(conta.saldo).toBeDefined();
   expect(conta.dataEncerramento).toBeNull();
   expect(conta.saca(-500).toThrow(Error);


              É possíve l negar um Matcher.


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




Saturday, May 14, 2011
Jasmine / Spies
   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();
         });

   });




Saturday, May 14, 2011
Jasmine / Spies
   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();
         });

   });




Saturday, May 14, 2011
Jasmine / Asynchronous specs
     describe("Testes Assicronos", function(){

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

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

     });




Saturday, May 14, 2011
Jasmine / Asynchronous specs
     describe("Testes Assicronos", function(){

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

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

     });




Saturday, May 14, 2011
Jasmine / Asynchronous specs
     describe("Testes Assicronos", function(){

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

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

     });




Saturday, May 14, 2011
Jasmine / Asynchronous specs




Saturday, May 14, 2011
Jasmine / Asynchronous specs
     describe("Testes Assicronos", function(){

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

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

     });




Saturday, May 14, 2011
Jasmine / Asynchronous specs
     describe("Testes Assicronos", function(){

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

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

     });




Saturday, May 14, 2011
Jasmine / Asynchronous specs
     describe("Testes Assicronos", function(){

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

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

     });




Saturday, May 14, 2011
Jasmine / Asynchronous specs
     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);
             });
           });

     });



Saturday, May 14, 2011
Jasmine / Asynchronous specs
     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);
             });
           });

     });



Saturday, May 14, 2011
Jasmine / Asynchronous specs
     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);
             });
           });

     });



Saturday, May 14, 2011
Jasmine / Asynchronous specs




Saturday, May 14, 2011
Jasmine / Asynchronous specs
     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);
         });
       });
     });
Saturday, May 14, 2011
Jasmine / Asynchronous specs
     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);
         });
       });
     });
Saturday, May 14, 2011
Jasmine / Asynchronous specs
     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);
         });
       });
     });
Saturday, May 14, 2011
Sinon.JS



Saturday, May 14, 2011
Sinon.js


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




Saturday, May 14, 2011
Sinon.js


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




Saturday, May 14, 2011
Sinon.js/Fake Timers
     describe("Sinon Examples", function(){

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

     });




Saturday, May 14, 2011
Sinon.js/Fake Timers
     describe("Sinon Examples", function(){

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

     });                                    dade
                                      eloci
                                  da v r!
                         Dep ende rowse
                               do b




Saturday, May 14, 2011
Sinon.js/Fake Timers
     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();

           });

     });



Saturday, May 14, 2011
Sinon.js/Fake Timers
     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();

           });

     });



Saturday, May 14, 2011
Sinon.js/Fake Timers
     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();

           });

     });



Saturday, May 14, 2011
Sinon.js/Fake Timers
     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();

           });

     });



Saturday, May 14, 2011
Sinon.js/Fake Timers
     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();
                                                          Each
                                                    after
           });                                ach e uteis.
                                          oreE ser
                                      bef
                                          podem
     });



Saturday, May 14, 2011
Sinon.js/Fake Server
    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"}]);
        );
      });
    });
Saturday, May 14, 2011
Sinon.js/Fake Server
    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"}]);
        );
      });
    });
Saturday, May 14, 2011
Sinon.js/Fake Server
    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"}]);
        );
      });
    });
Saturday, May 14, 2011
Sinon.js/Fake Server
    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"}]);
        );
      });
    });
Saturday, May 14, 2011
Sinon.js/Fake Server
    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"}]);
        );
      });
    });
Saturday, May 14, 2011
Referências




          •    JasmineBDD
                •    http://pivotal.github.com/jasmine/
          •    Sinon.js
                •    http://sinonjs.org/




Saturday, May 14, 2011
intelie.com/trabalhe




                          Obrigado
                         sergio.azevedo@intelie.com.br
                             falcao@intelie.com.br

Saturday, May 14, 2011

Mais conteúdo relacionado

Destaque

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destaque (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Jasmine - Sinon - BrazilJS

  • 1. Jasmine / Sinon.js Jorge Falcão Sergio Azevedo sergio.azevedo@intelie.com.br @sergioazevedo Saturday, May 14, 2011
  • 7. Jasmine / Spec //# ContaSpec.js # describe('Conta', function(){ it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); }); Saturday, May 14, 2011
  • 8. Jasmine / Spec //# ContaSpec.js # describe('Conta', function(){ Suite it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); }); Saturday, May 14, 2011
  • 9. Jasmine / Spec //# ContaSpec.js # describe('Conta', function(){ it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); }); Saturday, May 14, 2011
  • 10. Jasmine / Spec //# ContaSpec.js # describe('Conta', function(){ it("deve realizar saques", function(){ Spec var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); }); Saturday, May 14, 2011
  • 11. Jasmine / Spec //# ContaSpec.js # describe('Conta', function(){ it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); }); Saturday, May 14, 2011
  • 12. Jasmine / Spec //# ContaSpec.js # describe('Conta', function(){ it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300);Expectation }); }); Saturday, May 14, 2011
  • 13. Jasmine / Spec //# ContaSpec.js # describe('Conta', function(){ it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); }); Saturday, May 14, 2011
  • 14. Jasmine / Spec //# ContaSpec.js # describe('Conta', function(){ it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); Matcher }); Saturday, May 14, 2011
  • 15. Jasmine / Spec //# ContaSpec.js # describe('Conta', function(){ it("deve realizar saques", function(){ var conta = new Conta({saldo:1000}); conta.saca(300) expect(conta.saldo()).toEqual(300); }); }); Saturday, May 14, 2011
  • 16. Jasmine / Spec 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); }); }); Saturday, May 14, 2011
  • 17. Jasmine / Spec 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); }); }); Saturday, May 14, 2011
  • 18. Jasmine / beforeEach 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); }); }); Saturday, May 14, 2011
  • 19. Jasmine / beforeEach 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); }); }); Saturday, May 14, 2011
  • 20. Jasmine / beforeEach describe('Conta', function(){ var conta; beforeEach(function() { var conta = new Conta({saldo:1000}); h() Eac }); ter o! af to 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); }); }); Saturday, May 14, 2011
  • 21. Jasmine / Nested Spec 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(){ ... }); }); }); Saturday, May 14, 2011
  • 22. Jasmine / Nested Spec 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(){ ... }); }); }); Saturday, May 14, 2011
  • 23. Jasmine / Matchers 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); É possíve l negar um Matcher. expect(conta.saldo).not.toEqual(40); expect(conta).not.toBe(“Mario”); ... Saturday, May 14, 2011
  • 24. Jasmine / Matchers Tamb expect(conta.saldo).toEqual(1000); cria ém é po r se ssív expect(conta).toBe(conta); u pr el Matc ópri expect(conta.titular).toMatch(/ana/); her o expect(conta.saldo).toBeDefined(); expect(conta.dataEncerramento).toBeNull(); expect(conta.saca(-500).toThrow(Error); É possíve l negar um Matcher. expect(conta.saldo).not.toEqual(40); expect(conta).not.toBe(“Mario”); ... Saturday, May 14, 2011
  • 25. Jasmine / Spies 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(); }); }); Saturday, May 14, 2011
  • 26. Jasmine / Spies 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(); }); }); Saturday, May 14, 2011
  • 27. Jasmine / Asynchronous specs describe("Testes Assicronos", function(){ it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300); expect(a).toEqual(2); }); }); Saturday, May 14, 2011
  • 28. Jasmine / Asynchronous specs describe("Testes Assicronos", function(){ it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300); expect(a).toEqual(2); }); }); Saturday, May 14, 2011
  • 29. Jasmine / Asynchronous specs describe("Testes Assicronos", function(){ it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300); expect(a).toEqual(2); }); }); Saturday, May 14, 2011
  • 30. Jasmine / Asynchronous specs Saturday, May 14, 2011
  • 31. Jasmine / Asynchronous specs describe("Testes Assicronos", function(){ it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300); expect(a).toEqual(2); }); }); Saturday, May 14, 2011
  • 32. Jasmine / Asynchronous specs describe("Testes Assicronos", function(){ it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300); expect(a).toEqual(2); }); }); Saturday, May 14, 2011
  • 33. Jasmine / Asynchronous specs describe("Testes Assicronos", function(){ it("exemplo com waits", function() { var a = 1; setTimeout(function() { a++; console.debug("somando"); }, 300); expect(a).toEqual(2); }); }); Saturday, May 14, 2011
  • 34. Jasmine / Asynchronous specs 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); }); }); }); Saturday, May 14, 2011
  • 35. Jasmine / Asynchronous specs 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); }); }); }); Saturday, May 14, 2011
  • 36. Jasmine / Asynchronous specs 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); }); }); }); Saturday, May 14, 2011
  • 37. Jasmine / Asynchronous specs Saturday, May 14, 2011
  • 38. Jasmine / Asynchronous specs 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); }); }); }); Saturday, May 14, 2011
  • 39. Jasmine / Asynchronous specs 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); }); }); }); Saturday, May 14, 2011
  • 40. Jasmine / Asynchronous specs 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); }); }); }); Saturday, May 14, 2011
  • 42. Sinon.js • Spies • Stubs • Mocks • Fake Timers • Fake XHR • Fake Server Saturday, May 14, 2011
  • 43. Sinon.js • Spies • Stubs • Mocks • Fake Timers • Fake XHR • Fake Server Saturday, May 14, 2011
  • 44. Sinon.js/Fake Timers describe("Sinon Examples", function(){ it('FakeTimers', function(){ var data = new Date(); expect(elapsedTime(myDate)).toEqual(2); }); }); Saturday, May 14, 2011
  • 45. Sinon.js/Fake Timers describe("Sinon Examples", function(){ it('FakeTimers', function(){ var data = new Date(); expect(elapsedTime(myDate)).toEqual(2); }); }); dade eloci da v r! Dep ende rowse do b Saturday, May 14, 2011
  • 46. Sinon.js/Fake Timers 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(); }); }); Saturday, May 14, 2011
  • 47. Sinon.js/Fake Timers 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(); }); }); Saturday, May 14, 2011
  • 48. Sinon.js/Fake Timers 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(); }); }); Saturday, May 14, 2011
  • 49. Sinon.js/Fake Timers 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(); }); }); Saturday, May 14, 2011
  • 50. Sinon.js/Fake Timers 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(); Each after }); ach e uteis. oreE ser bef podem }); Saturday, May 14, 2011
  • 51. Sinon.js/Fake Server 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"}]); ); }); }); Saturday, May 14, 2011
  • 52. Sinon.js/Fake Server 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"}]); ); }); }); Saturday, May 14, 2011
  • 53. Sinon.js/Fake Server 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"}]); ); }); }); Saturday, May 14, 2011
  • 54. Sinon.js/Fake Server 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"}]); ); }); }); Saturday, May 14, 2011
  • 55. Sinon.js/Fake Server 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"}]); ); }); }); Saturday, May 14, 2011
  • 56. Referências • JasmineBDD • http://pivotal.github.com/jasmine/ • Sinon.js • http://sinonjs.org/ Saturday, May 14, 2011
  • 57. intelie.com/trabalhe Obrigado sergio.azevedo@intelie.com.br falcao@intelie.com.br Saturday, May 14, 2011

Notas do Editor

  1. estrutura basica de uma suite do Jasminie
  2. A ideia é ter varias spec’s dentro da suite.
  3. podemos evitar repticoes de codigo com blocos beforeEach
  4. before e after each serão uteis no fake server e fake timers
  5. -> é legal falar que agrupar as descrição pode agrupar os BeforeEach...
  6. existem outros -- dá uma olhada em como adicionar novos matchers https://github.com/pivotal/jasmine/wiki/Matchers
  7. https://github.com/pivotal/jasmine/wiki/Spies Para garantir que uma sequencia de passos estah sendo executada. Pode ser bom para testar graficos ou apis que geram imagens. É dificil fazer um “assert” na imagem, mas com spies vc pode garantri que a sequencia de chamadas de metodos necessarias para criacao da imagem está sendo executada na ordem correta.
  8. Para testar coisas assicronas, podemos usar runs e waits
  9. Para testar coisas assicronas, podemos usar runs e waits
  10. Para testar coisas assicronas, podemos usar runs e waits
  11. Para testar coisas assicronas, podemos usar runs e waits
  12. Para testar coisas assicronas, podemos usar runs e waits
  13. Para testar coisas assicronas, podemos usar runs e waits
  14. não vamos falar de stubs/mocks/spies no sinon.
  15. entre IE e Chrome o tempo será diferente por questoes de performance.
  16. entre IE e Chrome o tempo será diferente por questoes de performance.
  17. sinon.spy é do sinon.