Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduccion a Jasmin

1,240 views

Published on

Published in: Technology
  • Login to see the comments

Introduccion a Jasmin

  1. 1. Testeando JavaScript con Jasmine Por: Rodrigo Quelca
  2. 2. Que es Jasmine?Jasmine es un Framework de pruebas unitarias para JavaScript.
  3. 3. Introducion.js
  4. 4. CaracteristicasJasmine es un Framework de pruebas unitarias para JavaScript.
  5. 5. Estructura.js describe("A suite", function() { var a=4; //javascript code it("contains spec with an expectation", function() { expect(true).toBe(true); }); }); Suites: describe Your Tests Specs
  6. 6. ejemplo.js HelloWorld.js function helloWorld() { return "Hello world!"; } spec/HelloWorld.js describe("Hello world", function() { it("says hello", function() { expect(helloWorld()).toEqual("Hello world!"); }); });
  7. 7. ejemplo.js SpecRunner.html function helloWorld() { return "Hello world!"; } <link rel="stylesheet" type="text/css" href="../jasmine.css"> <script type="text/javascript" src="../jasmine.js"></script> <script type="text/javascript" src="../jasmine-html.js"></script> <!-- include spec files here... --> <script type="text/javascript" src="spec/HelloWorldSpec.js"></script> <!-- include source files here... --> <script type="text/javascript" src="src/HelloWorld.js"></script>
  8. 8. Algunos Comparadores expect(x).toEqual(y) expect(x).toBe(y); expect(x).toMatch(pattern); //regexp expect(x).toBeDefined(); expect(x).toBeNull(); expect(x).toBeTruthy(); expect(x).toBeFalsy(); expect(x).toContain(y); expect(x).toBeLessThan(y); expect(x).toBeGreaterThan(y); expect(fn).toThrow(e); expect(x).not.toEqual(y)
  9. 9. Comparadores personalizados describe(Hello world, function() { beforeEach(function() { this.addMatchers({ toBeDivisibleByTwo: function() { return (this.actual % 2) === 0; } }); }); it(is divisible by 2, function() { expect(gimmeANumber()).toBeDivisibleByTwo(); }); });
  10. 10. Before y after describe("A spec (with setup and tear-down)", function() { var foo; beforeEach(function() { foo = 0; foo += 1; }); afterEach(function() { foo = 0; }); it("is just a function, so it can contain any code", function() { expect(foo).toEqual(1); }); it("can have more than one expectation", function() { expect(foo).toEqual(1); expect(true).toEqual(true); }); });
  11. 11. Spies var Person = function() {}; Person.prototype.helloSomeone = function(toGreet) { return this.sayHello() + " " + toGreet; }; Person.prototype.sayHello = function() { return "Hello"; }; describe("Person", function() { it("calls the sayHello() function", function() { var fakePerson = new Person(); spyOn(fakePerson, "sayHello"); fakePerson.helloSomeone("world"); expect(fakePerson.sayHello).toHaveBeenCalled(); }); });
  12. 12. Creando Spies var Person = function() {}; Person.prototype.helloSomeone = function(toGreet) { return this.sayHello() + " " + toGreet; }; Person.prototype.sayHello = function() { return "Hello"; }; describe("Person", function() { it("says hello", function() { var fakePerson = new Person(); fakePerson.sayHello = jasmine.createSpy("Say-hello spy"); fakePerson.helloSomeone("world"); expect(fakePerson.sayHello).toHaveBeenCalled(); }); });
  13. 13. Tests asincronos(run(),waitsFor()) describe("Calculator", function() { it("should factor two huge numbers asynchronously", function() { var calc = new Calculator(); var answer = calc.factor(18973547201226, 28460320801839); expect(answer).toEqual(9486773600613); // DANGER ZONE: This doesnt work if factor() is asynchronous!! // THIS DOESNT WORK, STUPID }); });
  14. 14. Tests asincronos(run(),waitsFor())describe("Calculator", function() { it("should factor two huge numbers asynchronously", function() { var calc = new Calculator(); var answer = calc.factor(18973547201226, 28460320801839); waitsFor(function() { return calc.answerHasBeenCalculated(); }, "It took too long to find those factors.", 10000); runs(function() { expect(answer).toEqual(9486773600613); }); });});
  15. 15. Tests jQuerydescribe(I add a ToDo, function () { var mocks = {}; beforeEach(function () { loadFixtures("index.html"); mocks.todo = "something fun"; $(#todo).val(mocks.todo); ToDo.setup(); }); it(should call the addToDo function when create is clicked, function () { spyOn(ToDo, addToDo); $(#create).click(); expect(ToDo.addToDo).toHaveBeenCalledWith(mocks.todo); });});
  16. 16. Referencias Pivotal Labs pagina oficial http://pivotal.github.com/jasmine/ How do I Jasmine http://evanhahn.com/?p=181 jasmine-jquery https://github.com/velesin/jasmine-jquery/ Testing jQuery plugins with Node.js and Jasmine http://digitalbush.com/2011/03/29/testing-jquery-plugins- with-node-js-and-jasmine/ Tests de JavaScript con Jasmine http://es.asciicasts.com/episodes/261-tests-de- javascript-con-jasmine
  17. 17. Manos a la obra ...
  18. 18. Gracias ...

×