Skip to content

Instantly share code, notes, and snippets.

@rssilva
Created February 1, 2013 01:07
Show Gist options
  • Save rssilva/4688297 to your computer and use it in GitHub Desktop.
Save rssilva/4688297 to your computer and use it in GitHub Desktop.
Adding some portugues comments on Jasmine Default Example PlayerSpec.js for my post on dailydevtips.com
//Aqui vamos descrever o teste para Player
describe("Player", function() {
//criamos as variáveis player e song
var player;
var song;
//beforeEach, como a tradução indica, cria um novo Player
//e um novo Song antes de cada teste
beforeEach(function() {
player = new Player();
song = new Song();
});
//Uma das coisas legais de Jasmine é que o teste quase pode
//ser "lido". É semanticamente muito agradável
//"Isso deve estar apto para tocar uma música"
it("should be able to play a Song", function() {
//Aqui temos o objeto player, que foi instanciado ali em cima no BeforeEach
//estamos chamando o método 'play' de 'player' e passando 'song' como argumento
//'song' também foi criado lá no BeforeEach
player.play(song);
//expect, ou seja, esperamos que depois que eu chamei o método
//'play' de 'player' passando um 'song',
//a propriedade 'currentlyPlayingSong' seja igual ao próprio objeto
//Se você olhar para o método 'play' no arquivo Player.js,
//verá que eu basicamente chamo-o para adicionar um 'song' à
//propriedade currentlyPlayingSong e a propriedade 'isPlaying'
//é setada como true
expect(player.currentlyPlayingSong).toEqual(song);
//A instrução abaixo visa demonstrar como fazer matchers personalizados
//Matchers nada mais são do que "métodos personalizados" para realizar
//seus testes. Vamos ignorá-lo por enquanto
//demonstrates use of custom matcher
expect(player).toBePlaying(song);
});
describe("when song has been paused", function() {
beforeEach(function() {
player.play(song);
player.pause();
});
it("should indicate that the song is currently paused", function() {
expect(player.isPlaying).toBeFalsy();
// demonstrates use of 'not' with a custom matcher
expect(player).not.toBePlaying(song);
});
it("should be possible to resume", function() {
player.resume();
expect(player.isPlaying).toBeTruthy();
expect(player.currentlyPlayingSong).toEqual(song);
});
});
// demonstrates use of spies to intercept and test method calls
it("tells the current song if the user has made it a favorite", function() {
spyOn(song, 'persistFavoriteStatus');
player.play(song);
player.makeFavorite();
expect(song.persistFavoriteStatus).toHaveBeenCalledWith(true);
});
//demonstrates use of expected exceptions
describe("#resume", function() {
it("should throw an exception if song is already playing", function() {
player.play(song);
expect(function() {
player.resume();
}).toThrow("song is already playing");
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment