summaryrefslogtreecommitdiff
path: root/spec/javascripts
diff options
context:
space:
mode:
authormo k <mo@mokhan.ca>2012-09-18 12:11:45 -0600
committermo k <mo@mokhan.ca>2012-09-18 12:11:45 -0600
commit0cf67e92f4d5c02863e34418ad4370375015a046 (patch)
tree3caa0720ec230524a82627d8e24b93b869bdffa1 /spec/javascripts
parent52cd080935d2f08653892ebe2e631453767d21cc (diff)
add jasmine examples.
Diffstat (limited to 'spec/javascripts')
-rw-r--r--spec/javascripts/helpers/SpecHelper.js9
-rw-r--r--spec/javascripts/jasmine_examples/PlayerSpec.js58
2 files changed, 67 insertions, 0 deletions
diff --git a/spec/javascripts/helpers/SpecHelper.js b/spec/javascripts/helpers/SpecHelper.js
new file mode 100644
index 00000000..4919c87a
--- /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/jasmine_examples/PlayerSpec.js b/spec/javascripts/jasmine_examples/PlayerSpec.js
new file mode 100644
index 00000000..79f10221
--- /dev/null
+++ b/spec/javascripts/jasmine_examples/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