• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Jasmine
 

Jasmine

on

  • 458 views

 

Statistics

Views

Total Views
458
Views on SlideShare
454
Embed Views
4

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 4

http://blog.moove-it.com 4

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Jasmine Jasmine Presentation Transcript

    • Jasmine
    • Qué es?
    • Qué es?"behavior-driven development framework fortesting JavaScript code"
    • Qué es?"behavior-driven development framework fortesting JavaScript code"http://github.com/pivotal/jasmine
    • Qué es?"behavior-driven development framework fortesting JavaScript code"http://github.com/pivotal/jasmine● sintaxis simple● no depende de otros frameworks js ni de existencia de DOM
    • Ejemplofunction helloWorld(){return Hello World!}describe(HelloWorld, function(){it(returns hello world message, function(){expect(helloWorld()).toEqual(Hello World!)});});
    • Ejemplo - Passing
    • Ejemplofunction helloWorld(){return Bye Bye World!}describe(HelloWorld, function(){it(returns hello world message, function(){expect(helloWorld()).toEqual(Hello World!)});});
    • Ejemplo - Failing
    • Suite - Spec - ExpectationUna suite está compuesta por:● al menos un bloque describe● que contiene bloques it (specs)● que contienen expectationsdescribe(HelloWorld, function(){it(returns hello world message, function(){expect(helloWorld()).toEqual(Hello World!)});});
    • Setup and Teardowndescribe(MyObject, function() {var obj = new MyObject();beforeEach(function() {obj.setState(fixed);});it(changes state, function() {obj.setState(broken);expect(obj.getState()).toEqual(broken);})it("adds states", function() {obj.addState(packaged);expect(obj.getState()).toEqual([fixed, packaged]);})});
    • Matchersexpects(x).toEqual(y)expects(x).toBe(y)expect(x).toContain(y)expect(x).toBeTruthy()expect(x).toBeFalsy(expect(x).toBeDefined()expect(x).toBeUndefined()expect(x).toBeNull()
    • Matchersexpects(x).toEqual(y)expects(x).toBe(y)expect(x).toBeTruthy()expect(x).toBeFalsy()expect(x).toBeDefined()expect(x).toBeUndefined()expect(x).toBeNull()expect(x).toContain(y)
    • Matchersexpects(x).toEqual(y)expects(x).toBe(y)expect(x).toBeTruthy()expect(x).toBeFalsy()expect(x).toBeDefined()expect(x).toBeUndefined()expect(x).toBeNull()expect(x).toContain(y)
    • Matchersexpects(x).toEqual(y)expects(x).toBe(y)expect(x).toBeTruthy()expect(x).toBeFalsy()expect(x).toBeDefined()expect(x).toBeUndefined()expect(x).toBeNull()expect(x).toContain(y)
    • Matchersexpect(x).toBeLessThan(y)expect(x).toBeGreaterThan(y)expect(x).toMatch(pattern);expect(function(){fn();}).toThrow(e);// todos los matchers se pueden invertirexpect(x).not.toEqual(y)expect(x).not.toMatch(pattern)
    • Matchersexpect(x).toBeLessThan(y)expect(x).toBeGreaterThan(y)expect(x).toMatch(pattern);expect(function(){fn();}).toThrow(e);// todos los matchers se pueden invertirexpect(x).not.toEqual(y)expect(x).not.toMatch(pattern)
    • Matchersexpect(x).toBeLessThan(y)expect(x).toBeGreaterThan(y)expect(x).toMatch(pattern);expect(function(){fn();}).toThrow(e);// todos los matchers se pueden invertirexpect(x).not.toEqual(y)expect(x).not.toMatch(pattern)
    • Matchersexpect(x).toBeLessThan(y)expect(x).toBeGreaterThan(y)expect(x).toMatch(pattern);expect(function(){fn();}).toThrow(e);// todos los matchers se pueden invertirexpect(x).not.toEqual(y)expect(x).not.toMatch(pattern)
    • Custom matchersdescribe(Numbers Module, function() {beforeEach(function() {this.addMatchers({toBeDivisibleByTwo: function() {return (this.actual % 2) === 0;}});});it(is divisible by 2, function() {expect(gimmeANumber()).toBeDivisibleByTwo();});});
    • Spies● test doubles
    • Spies● test doubles● reportar que fueron invocados
    • Spies● test doubles● reportar que fueron invocados● reportar cuantas veces fueron invocados
    • Spies● test doubles● reportar que fueron invocados● reportar cuantas veces fueron invocados● reportar con qué parámetros fueron invocados
    • Spies● test doubles● reportar que fueron invocados● reportar cuantas veces fueron invocados● reportar con qué parámetros fueron invocados● si se quiere, invocar al método que están reemplazando
    • Spies - Ejemplofunction Player() {this.isPlaying = false;}Player.prototype.play = function(song) {this.currentlyPlayingSong = song;this.isPlaying = true;};Player.prototype.pause = function() {this.isPlaying = false;};
    • Spies - EjemploPlayer.prototype.togglePlay = function(song) {if (this.isPlaying) {this.pause();}else {this.play(song);}}
    • Spies - Ejemplodescribe(tooglePlay, function() {beforeEach(function() {player.play(song);});it(should pause if its playing, function() {spyOn(player, pause); // define the spyplayer.togglePlay(song);expect(player.pause).toHaveBeenCalled();});});
    • Spies - ComportamientoLos spies pueden configurarse para responder de diferentesmaneras cuando son invocados:
    • Spies - Comportamiento// spies on AND calls the original function spied onspyOn(x, method).andCallThrough()Los spies pueden configurarse para responder de diferentesmaneras cuando son invocados:
    • Spies - Comportamiento// spies on AND calls the original function spied onspyOn(x, method).andCallThrough()// returns passed arguments when spy is calledspyOn(x, method).andReturn(arguments)Los spies pueden configurarse para responder de diferentesmaneras cuando son invocados:
    • Spies - Comportamiento// spies on AND calls the original function spied onspyOn(x, method).andCallThrough()// returns passed arguments when spy is calledspyOn(x, method).andReturn(arguments)// throws passed exception when spy is calledspyOn(x, method).andThrow(exception)Los spies pueden configurarse para responder de diferentesmaneras cuando son invocados:
    • Spies - Comportamiento// spies on AND calls the original function spied onspyOn(x, method).andCallThrough()// returns passed arguments when spy is calledspyOn(x, method).andReturn(arguments)// throws passed exception when spy is calledspyOn(x, method).andThrow(exception)// calls passed function when spy is calledspyOn(x, method).andCallFake(function)Los spies pueden configurarse para responder de diferentesmaneras cuando son invocados:
    • Spies - Matchers y propiedadesexpect(x.method).toHaveBeenCalled()expect(x.method).toHaveBeenCalledWith(args)matchers
    • Spies - Matchers y propiedadescallCountmostRecentCall.argsargsForCall[i]expect(x.method).toHaveBeenCalled()expect(x.method).toHaveBeenCalledWith(args)matcherspropiedades
    • Spies - Matchers y propiedadescallCountmostRecentCall.argsargsForCall[i]expect(x.method.callCount).toEqual(1)expect(x.method).toHaveBeenCalled()expect(x.method).toHaveBeenCalledWith(args)matcherspropiedades
    • Ajax - EjemploPlayer.prototype.getNewSongs = function() {var _this = this;$.ajax({url: /songs,dataType: json,success: function(data) {_this.displaySongs(data.songs);},error: function(data) {_this.displayErrorMessage();}});}
    • Ajax - Ejemploit(displays songs if getNewSongs is successful, function() {var player = new Player();spyOn($, ajax).andCallFake(function(e) {e.success({songs: [Some]});});spyOn(player, displaysSongs);player.getNewSongs();expect(player.displaySongs).toHaveBeenCalledWith([Some]);});
    • Mocking JavaScript ClockbeforeEach(function() {timerCallback = jasmine.createSpy(timerCallback);jasmine.Clock.useMock();});it(causes a timeout to be called synchronously, function() {setTimeout(function() { timerCallback(); }, 100);expect(timerCallback).not.toHaveBeenCalled();jasmine.Clock.tick(101);expect(timerCallback).toHaveBeenCalled();});
    • Asynchronous Supportvar foo = 0;it(should be an async test, function() {runs(function() {setTimeout(function() { foo++ }, 250);});waits(500);runs(function() {expect(foo).toEqual(1);});});
    • jasmine-jquerycustom matchers para usar con jqueryexpect($(<div id="some-id"></div>)).toBe(div)var $div = $(<div><span class="a-class"></span></div>)expect($div).toContain(span.a-class)var $div = $(<div><h1>header</h1></div>)expect($div).toContainText(header)
    • jasmine-jqueryloadFixtures(my_fixture.html);manejo de fixtures html, css y jsonloadStyleFixtures(my_fixture.css);var data = getJSONFixture(my_json_fixture.json);
    • jasmine-jquery - Ejemplo<div id="player-content">some complex content</div>loadFixtures(player_fixture.html);player.init();expect($(#player-content)).to...;player_fixture.html
    • Para tener en cuenta...jasmine gemhttps://github.com/pivotal/jasmine-gembrowser + rake tasksinon.jshttp://sinonjs.org/Fake XMLHttpRequest, Fake Servertry jasminehttp://tryjasmine.com/
    • Preguntas?