Skip to content

Commit

Permalink
Added Jasmine for Javascript testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Elkin committed Sep 11, 2011
1 parent 4c21df5 commit 642c696
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ group :test do
gem 'rspec-rails', '1.3.4'
gem 'webrat', '0.7.1'
gem 'capybara', '1.0.1'
gem 'jasmine'
end

6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ GEM
activesupport
builder
hpricot (0.8.2)
jasmine (1.0.2.1)
json_pure (>= 1.4.3)
rack (>= 1.1)
rspec (>= 1.3.1)
selenium-webdriver (>= 0.1.3)
json (1.4.6)
json_pure (1.4.6)
launchy (0.4.0)
Expand Down Expand Up @@ -116,6 +121,7 @@ DEPENDENCIES
fastercsv (= 1.5.3)
hoptoad_notifier (= 2.4.2)
hpricot (= 0.8.2)
jasmine
json (= 1.4.6)
json_pure (= 1.4.6)
launchy (= 0.4.0)
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/jasmine.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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
22 changes: 22 additions & 0 deletions public/javascripts/Player.js
Original file line number Diff line number Diff line change
@@ -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);
};
7 changes: 7 additions & 0 deletions public/javascripts/Song.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Song() {
}

Song.prototype.persistFavoriteStatus = function(value) {
// something complicated
throw new Error("not yet implemented");
};
9 changes: 9 additions & 0 deletions spec/javascripts/helpers/SpecHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
beforeEach(function() {
this.addMatchers({
toBePlaying: function(expectedSong) {
var player = this.actual;
return player.currentlyPlayingSong === expectedSong
&& player.isPlaying;
}
})
});
58 changes: 58 additions & 0 deletions spec/javascripts/manage_photos.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
describe("Manage Photos", 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");
});
});
});
81 changes: 81 additions & 0 deletions spec/javascripts/support/jasmine.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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/prototype.js
- public/javascripts/effects.js
- public/javascripts/controls.js
- public/javascripts/dragdrop.js
- public/javascripts/application.js
- 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:
- stylesheets/**/*.css

# helpers
#
# Return an array of filepaths relative to spec_dir to include before jasmine specs.
# Default: ["helpers/**/*.js"]
#
# EXAMPLE:
#
# helpers:
# - helpers/**/*.js
#
helpers:
- helpers/**/*.js

# 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:
- '**/*[sS]pec.js'

# 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: spec/javascripts
23 changes: 23 additions & 0 deletions spec/javascripts/support/jasmine_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Jasmine
class Config

# Add your overrides or custom config code here

end
end


# Note - this is necessary for rspec2, which has removed the backtrace
module Jasmine
class SpecBuilder
def declare_spec(parent, spec)
me = self
example_name = spec["name"]
@spec_ids << spec["id"]
backtrace = @example_locations[parent.description + " " + example_name]
parent.it example_name, {} do
me.report_spec(spec["id"])
end
end
end
end
32 changes: 32 additions & 0 deletions spec/javascripts/support/jasmine_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
$:.unshift(ENV['JASMINE_GEM_PATH']) if ENV['JASMINE_GEM_PATH'] # for gem testing purposes

require 'rubygems'
require 'jasmine'
jasmine_config_overrides = File.expand_path(File.join(File.dirname(__FILE__), 'jasmine_config.rb'))
require jasmine_config_overrides if File.exist?(jasmine_config_overrides)
if Jasmine::rspec2?
require 'rspec'
else
require 'spec'
end

jasmine_config = Jasmine::Config.new
spec_builder = Jasmine::SpecBuilder.new(jasmine_config)

should_stop = false

if Jasmine::rspec2?
RSpec.configuration.after(:suite) do
spec_builder.stop if should_stop
end
else
Spec::Runner.configure do |config|
config.after(:suite) do
spec_builder.stop if should_stop
end
end
end

spec_builder.start
should_stop = true
spec_builder.declare_suites

0 comments on commit 642c696

Please sign in to comment.