Javascript测试框架Jasmine(一):简介

测试报告会自动追加到页尾

  1. <script type="text/javascript">
  2. function Player() {
  3. }
  4. Player.prototype.play = function (song) {
  5. this.currentlyPlayingSong = song;
  6. this.isPlaying = true;
  7. };
  8.  
  9. Player.prototype.pause = function () {
  10. this.isPlaying = false;
  11. };
  12.  
  13. Player.prototype.resume = function () {
  14. if (this.isPlaying) {
  15. throw new Error("song is already playing");
  16. }
  17.  
  18. this.isPlaying = true;
  19. };
  20.  
  21. Player.prototype.makeFavorite = function () {
  22. this.currentlyPlayingSong.persistFavoriteStatus(true);
  23. };
  24. </script>
  25. <script type="text/javascript">
  26. function Song() {
  27. }
  28.  
  29. Song.prototype.persistFavoriteStatus = function (value) {
  30. // something complicated
  31. throw new Error("not yet implemented");
  32. };
  33. </script>
  34.  
  35. <!-- include spec files here... -->
  36. <script type="text/javascript">
  37. beforeEach(function () {
  38. jasmine.addMatchers({
  39. toBePlaying: function () {
  40. return {
  41. compare: function (actual, expected) {
  42. var player = actual;
  43.  
  44. return {
  45. pass: player.currentlyPlayingSong === expected && player.isPlaying
  46. }
  47. }
  48. };
  49. }
  50. });
  51. });
  52. </script>
  53. <script type="text/javascript">
  54. describe("Player", function () {
  55. var player;
  56. var song;
  57.  
  58. beforeEach(function () {
  59. player = new Player();
  60. song = new Song();
  61. });
  62.  
  63. it("should be able to play a Song", function () {
  64. player.play(song);
  65. expect(player.currentlyPlayingSong).toEqual(song);
  66.  
  67. //demonstrates use of custom matcher
  68. expect(player).toBePlaying(song);
  69. });
  70.  
  71. describe("when song has been paused", function () {
  72. beforeEach(function () {
  73. player.play(song);
  74. player.pause();
  75. });
  76.  
  77. it("should indicate that the song is currently paused", function () {
  78. expect(player.isPlaying).toBeFalsy();
  79.  
  80. // demonstrates use of 'not' with a custom matcher
  81. expect(player).not.toBePlaying(song);
  82. });
  83.  
  84. it("should be possible to resume", function () {
  85. player.resume();
  86. expect(player.isPlaying).toBeTruthy();
  87. expect(player.currentlyPlayingSong).toEqual(song);
  88. });
  89. });
  90.  
  91. // demonstrates use of spies to intercept and test method calls
  92. it("tells the current song if the user has made it a favorite", function () {
  93. spyOn(song, 'persistFavoriteStatus');
  94.  
  95. player.play(song);
  96. player.makeFavorite();
  97.  
  98. expect(song.persistFavoriteStatus).toHaveBeenCalledWith(true);
  99. });
  100.  
  101. //demonstrates use of expected exceptions
  102. describe("#resume", function () {
  103. it("should throw an exception if song is already playing", function () {
  104. player.play(song);
  105.  
  106. expect(function () {
  107. player.resume();
  108. }).toThrowError("song is already playing");
  109. });
  110. });
  111. });
  112. </script>
5 specs, 0 failures