PlayerSpec.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. describe("Player", function() {
  2. var player;
  3. var song;
  4. beforeEach(function() {
  5. player = new Player();
  6. song = new Song();
  7. });
  8. it("should be able to play a Song", function() {
  9. player.play(song);
  10. expect(player.currentlyPlayingSong).toEqual(song);
  11. //demonstrates use of custom matcher
  12. expect(player).toBePlaying(song);
  13. });
  14. describe("when song has been paused", function() {
  15. beforeEach(function() {
  16. player.play(song);
  17. player.pause();
  18. });
  19. it("should indicate that the song is currently paused", function() {
  20. expect(player.isPlaying).toBeFalsy();
  21. // demonstrates use of 'not' with a custom matcher
  22. expect(player).not.toBePlaying(song);
  23. });
  24. it("should be possible to resume", function() {
  25. player.resume();
  26. expect(player.isPlaying).toBeTruthy();
  27. expect(player.currentlyPlayingSong).toEqual(song);
  28. });
  29. });
  30. // demonstrates use of spies to intercept and test method calls
  31. it("tells the current song if the user has made it a favorite", function() {
  32. spyOn(song, 'persistFavoriteStatus');
  33. player.play(song);
  34. player.makeFavorite();
  35. expect(song.persistFavoriteStatus).toHaveBeenCalledWith(true);
  36. });
  37. //demonstrates use of expected exceptions
  38. describe("#resume", function() {
  39. it("should throw an exception if song is already playing", function() {
  40. player.play(song);
  41. expect(function() {
  42. player.resume();
  43. }).toThrow("song is already playing");
  44. });
  45. });
  46. });