Introduccion a Jasmin
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,111
On Slideshare
1,111
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
12
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Testeando JavaScript con Jasmine Por: Rodrigo Quelca
  • 2. Que es Jasmine?Jasmine es un Framework de pruebas unitarias para JavaScript.
  • 3. Introducion.js
  • 4. CaracteristicasJasmine es un Framework de pruebas unitarias para JavaScript.
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Manos a la obra ...
  • 18. Gracias ...