Testing your Rails Javascript
with Mocha, Chai, and Konacha
by Brian Auton
Developer and consultant at
function closePopup() {
$("#popup").remove();
}
suite("closePopup", function() {
setup(function() {
$("body").append("<div id='popup'></div>");
});
test("should close the popup", function() {
closePopup();
if($("#popup").is(":visible")) throw "failure";
});
});
function closePopup() {
$("#popup").remove();
}
suite "closePopup", ->
setup ->
$("body").append("<div id='popup'></div>");
test "should close the popup", ->
closePopup()
throw "failure" if $("#popup").is ":visible"
assert = chai.assert
suite "closePopup", ->
setup ->
$("body").append "<div id='popup'></div>"
test "should close the popup", ->
closePopup()
assert.equal false, $("#popup").is(":visible")
should = chai.should()
suite "closePopup", ->
setup ->
$("body").append "<div id='popup'></div>"
test "should close the popup", ->
closePopup()
$("#popup").is(":visible").should.equal false
expect = chai.expect
suite "closePopup", ->
setup ->
$("body").append "<div id='popup'></div>"
test "should close the popup", ->
closePopup()
expect($("#popup").is(":visible")).to.equal false
mocha.ui "bdd"
assert = chai.assert
describe "closePopup", ->
beforeEach ->
$("body").append "<div id='popup'></div>"
it "should close the popup", ->
closePopup()
assert.equal false, $("#popup").is(":visible")
function closePopup(callback) {
$("#popup").fadeOut(400);
}
assert = chai.assert
suite "closePopup", ->
setup ->
$("body").append "<div id='popup'></div>"
test "should close the popup", (done)->
closePopup()
assert.equal true, $("#popup").is(":visible")
setTimeout ->
assert.equal false, $("#popup").is(":visible")
done()
, 500
group :test, :development do
gem 'konacha'
end
Konacha.configure do |config|
config.spec_dir = "test/javascripts"
end
http://phantomjs.org/download.html
group :test, :development do
gem 'konacha'
gem 'poltergeist', :require => "capybara/poltergeist"
end
Konacha.configure do |config|
config.spec_dir = "test/javascripts"
config.driver = :poltergeist
end
mocha.ui "tdd"
mocha.globals ["$"]
#= require test_helper
#= require application
suite "closePopup", ->
setup ->
$("body").append "<div id='popup'></div>"
test "should close the popup", (done)->
closePopup()
assert.equal true, $("#popup").is(":visible")
setTimeout ->
assert.equal false, $("#popup").is(":visible")
done()
, 500
$ rake konacha:run . Finished in 0.41 seconds 1 examples, 0 failed, 0 pending
task "test:javascripts" => "konacha:run"
task "test" => [
"test:units",
"test:functionals",
"test:integration",
"test:javascripts"
]
$ rake test:javascripts
$ rake
For another talk...