Skip to content

Instantly share code, notes, and snippets.

@nathanic
Created September 13, 2011 19:01
Show Gist options
  • Save nathanic/1214725 to your computer and use it in GitHub Desktop.
Save nathanic/1214725 to your computer and use it in GitHub Desktop.
jasmine BDD example
describe("Player", function() {
var player;
// hook before each test
beforeEach(function(){
player = new Player();
spyOn(loading,'show'); // intercept and record calls to loading.show
player.play();
});
// actual specifications/tests
it('shows a loading indicator',function(){
expect(loading.show).toHaveBeenCalled();
});
it('plays',function(){
expect(player.isPlaying).toBe(true);
});
// specs may be nested recursively
describe('pausing',function(){
beforeEach(function(){
player.pause();
});
it('pauses',function() {
expect(player).not.toBePlaying();
});
});
});
@nathanic
Copy link
Author

Note: toBePlaying is implemented as a custom expect hook in another file.

  beforeEach(function() {
    this.addMatchers({
      toBePlaying: function(expectedSong) {
        var player = this.actual;
        return player.currentlyPlayingSong === expectedSong
            && player.isPlaying;
      }
    })
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment