November 21, 2014
Hot Topics:

BDD Framework for JavaScript Testing, Page 2

  • August 9, 2011
  • By Jason Gilmore
  • Send Email »
  • More Articles »

Testing the Player Class

When executed, the Player class' play method assigns values to the currentlyPlayingSong and isPlaying properties. Let's write a test to ensure the currentlyPlayingSong property is indeed set to the proper song. The song in question is an object instantiated via the Song class (found in the public/javascripts directory) passed into the Player class' play method

it('The Player is playing the proper song', function() {

  var player = new Player();
  var song = new Song();

  player.play(song);
  expect(player.currentlyPlayingSong).toEqual(song);

});

Refresh the browser and you'll see this spec does indeed pass. But we should also ensure that the isPlaying property is also set to true. You could perform the expectation test into the above spec, however perhaps breaking it out into its own spec is a better idea. When you create multiple specs intended to test one specific behavior (in our case, the behavior of the play method), you should create a suite.

Creating a Spec Suite

A spec suite is organized in a manner such that it is obvious a particular group of specs are intended to thoroughly test a particular behavior. This is done by nesting the related specs inside the describe() function:

describe("Play2", function() {

  describe("when song is being played", function() {

    it('The Player knows it is playing', function() {

      var player = new Player();
      var song = new Song();

      player.play(song);
      expect(player.isPlaying).toBe(true);

    });

    it('The Player is playing the proper song', function() {

      var player = new Player();
      var song = new Song();

      player.play(song);
      expect(player.currentlyPlayingSong).toEqual(song);

    });

  });

});

Refresh the browser and you'll see that not only have both tests passed, but they have also been grouped in such a way that it is apparent they are intended to test a specific behavior. But the specs aren't DRY, which will inevitably come back to bite us at one point. Use the beforeEach() function to solve this problem:

describe("when song is being played", function() {

  beforeEach(function() {
    player = new Player();
    song = new Song();
  });

  it('The Player knows it is playing', function() {

    player.play(song);
    expect(player.isPlaying).toBe(true);

  });

  it('The Player is playing the proper song', function() {

    player.play(song);
    expect(player.currentlyPlayingSong).toEqual(song);

  });

});

Conclusion

Of the various BDD frameworks I've used over the years, Jasmine is by far my favorite, offering an incredibly intuitive and natural testing approach. Hopefully this article helped to illustrate some of the features I find so appealing. Are you using Jasmine or other JavaScript testing frameworks? Tell us about your experiences in the comments!



Originally published on http://www.developer.com.

Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel