diff options
| author | mo k <mo@mokhan.ca> | 2012-05-21 10:39:28 -0600 |
|---|---|---|
| committer | mo k <mo@mokhan.ca> | 2012-05-21 10:39:28 -0600 |
| commit | 1d0e2de27d8716cdfc409e1d16157aa2e53e4f98 (patch) | |
| tree | 668be6211dd0af810325705a58ad29a6f0f498fc | |
| parent | f02ef42c41eba8d103bb0a73d10839d6f1f64189 (diff) | |
jasmine init
| -rw-r--r-- | Rakefile | 9 | ||||
| -rw-r--r-- | public/javascripts/Player.js | 22 | ||||
| -rw-r--r-- | public/javascripts/Song.js | 7 | ||||
| -rw-r--r-- | spec/javascripts/PlayerSpec.js | 58 | ||||
| -rw-r--r-- | spec/javascripts/helpers/SpecHelper.js | 9 | ||||
| -rw-r--r-- | spec/javascripts/support/jasmine.yml | 73 |
6 files changed, 178 insertions, 0 deletions
diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..d673004 --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ + +begin + require 'jasmine' + load 'jasmine/tasks/jasmine.rake' +rescue LoadError + task :jasmine do + abort "Jasmine is not available. In order to run jasmine, you must: (sudo) gem install jasmine" + end +end diff --git a/public/javascripts/Player.js b/public/javascripts/Player.js new file mode 100644 index 0000000..fcce826 --- /dev/null +++ b/public/javascripts/Player.js @@ -0,0 +1,22 @@ +function Player() { +} +Player.prototype.play = function(song) { + this.currentlyPlayingSong = song; + this.isPlaying = true; +}; + +Player.prototype.pause = function() { + this.isPlaying = false; +}; + +Player.prototype.resume = function() { + if (this.isPlaying) { + throw new Error("song is already playing"); + } + + this.isPlaying = true; +}; + +Player.prototype.makeFavorite = function() { + this.currentlyPlayingSong.persistFavoriteStatus(true); +};
\ No newline at end of file diff --git a/public/javascripts/Song.js b/public/javascripts/Song.js new file mode 100644 index 0000000..a8a3f2d --- /dev/null +++ b/public/javascripts/Song.js @@ -0,0 +1,7 @@ +function Song() { +} + +Song.prototype.persistFavoriteStatus = function(value) { + // something complicated + throw new Error("not yet implemented"); +};
\ No newline at end of file diff --git a/spec/javascripts/PlayerSpec.js b/spec/javascripts/PlayerSpec.js new file mode 100644 index 0000000..79f1022 --- /dev/null +++ b/spec/javascripts/PlayerSpec.js @@ -0,0 +1,58 @@ +describe("Player", function() { + var player; + var song; + + beforeEach(function() { + player = new Player(); + song = new Song(); + }); + + it("should be able to play a Song", function() { + player.play(song); + expect(player.currentlyPlayingSong).toEqual(song); + + //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"); + }); + }); +});
\ No newline at end of file diff --git a/spec/javascripts/helpers/SpecHelper.js b/spec/javascripts/helpers/SpecHelper.js new file mode 100644 index 0000000..e9b8284 --- /dev/null +++ b/spec/javascripts/helpers/SpecHelper.js @@ -0,0 +1,9 @@ +beforeEach(function() { + this.addMatchers({ + toBePlaying: function(expectedSong) { + var player = this.actual; + return player.currentlyPlayingSong === expectedSong && + player.isPlaying; + } + }); +}); diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml new file mode 100644 index 0000000..7cce692 --- /dev/null +++ b/spec/javascripts/support/jasmine.yml @@ -0,0 +1,73 @@ +# src_files +# +# Return an array of filepaths relative to src_dir to include before jasmine specs. +# Default: [] +# +# EXAMPLE: +# +# src_files: +# - lib/source1.js +# - lib/source2.js +# - dist/**/*.js +# +src_files: + - public/javascripts/**/*.js + +# stylesheets +# +# Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs. +# Default: [] +# +# EXAMPLE: +# +# stylesheets: +# - css/style.css +# - stylesheets/*.css +# +stylesheets: + +# helpers +# +# Return an array of filepaths relative to spec_dir to include before jasmine specs. +# Default: ["helpers/**/*.js"] +# +# EXAMPLE: +# +# helpers: +# - helpers/**/*.js +# +helpers: + +# spec_files +# +# Return an array of filepaths relative to spec_dir to include. +# Default: ["**/*[sS]pec.js"] +# +# EXAMPLE: +# +# spec_files: +# - **/*[sS]pec.js +# +spec_files: + +# src_dir +# +# Source directory path. Your src_files must be returned relative to this path. Will use root if left blank. +# Default: project root +# +# EXAMPLE: +# +# src_dir: public +# +src_dir: + +# spec_dir +# +# Spec directory path. Your spec_files must be returned relative to this path. +# Default: spec/javascripts +# +# EXAMPLE: +# +# spec_dir: spec/javascripts +# +spec_dir: |
