Created
September 3, 2011 21:04
-
-
Save GraemeF/1191793 to your computer and use it in GitHub Desktop.
Jasmine example converted to CoffeeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Player | |
play: (song) -> | |
@currentlyPlayingSong = song | |
@isPlaying = true | |
pause: -> | |
@isPlaying = false | |
resume: -> | |
throw new Error "song is already playing" if this.isPlaying | |
@isPlaying = true | |
makeFavorite: -> | |
@currentlyPlayingSong.persistFavoriteStatus true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "Player", -> | |
player = null | |
song = null | |
beforeEach -> | |
player = new Player() | |
song = new Song() | |
it "should be able to play a Song", -> | |
player.play song | |
expect(player.currentlyPlayingSong).toEqual song | |
# demonstrates use of custom matcher | |
expect(player).toBePlaying song | |
describe "when song has been paused", -> | |
beforeEach -> | |
player.play song | |
player.pause() | |
it "should indicate that the song is currently paused", -> | |
expect(player.isPlaying).toBeFalsy() | |
# demonstrates use of 'not' with a custom matcher | |
expect(player).not.toBePlaying song | |
it "should be possible to resume", -> | |
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", -> | |
spyOn song, 'persistFavoriteStatus' | |
player.play song | |
player.makeFavorite() | |
expect(song.persistFavoriteStatus).toHaveBeenCalledWith true | |
# demonstrates use of expected exceptions | |
describe "#resume", -> | |
it "should throw an exception if song is already playing", -> | |
player.play song | |
expect(-> player.resume()).toThrow "song is already playing" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Song | |
persistFavoriteStatus: -> | |
# something complicated | |
throw new Error "not yet implemented" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach -> | |
@addMatchers { | |
toBePlaying: (expectedSong) -> | |
player = @actual | |
player.currentlyPlayingSong == expectedSong and player.isPlaying | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment