Skip to content

Instantly share code, notes, and snippets.

@tooky
Created March 2, 2010 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tooky/319235 to your computer and use it in GitHub Desktop.
Save tooky/319235 to your computer and use it in GitHub Desktop.

Exploring Harmony for unit testing JS with RSpec.

Installation

gem install rspec

# Note: there's a gem dependency bug in rubygems currently, so we'll have
# to install some dependencies manually. This will be fixed soon.
gem install stackdeck
gem install johnson -v "2.0.0.pre0" #exact version matters

gem install harmony

Run

spec rspec_with_harmony.rb
$('document').ready( function() {
$('body').addClass("js");
});
$('#widget').click( function() {
$(this).after('<div id="extra">BOOM!</div>');
});
require 'rubygems'
require 'spec'
require 'harmony'
require 'pathname'
current_dir = Pathname.new File.expand_path(File.dirname(__FILE__))
page = Harmony::Page.fetch(current_dir + "test.html")
describe "harmony" do
it "can execute simple js" do
page.x('2+2').should == 4
end
it "can query the dom" do
page.x('$("#widget").text()').should == "O HAI!"
end
it "can load javascript into the page" do
page.x('$("body").hasClass("js")').should be_false
page.load(current_dir + 'add_class_to_body.js')
page.x('$("body").hasClass("js")').should be_true
end
it "can interact with the dom" do
page.x('$("#extra").size()').should == 0
page.load(current_dir + 'add_text_on_click.js')
page.x('$("#widget").click()')
page.x('$("#extra").size()').should == 1
end
end
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.pack.js" type="text/javascript"></script>
<title>HTML Fixture for Harmony with rSpec</title>
</head>
<body>
<div id="widget">O HAI!</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment