Created
November 12, 2010 22:08
-
-
Save kfitzpatrick/674787 to your computer and use it in GitHub Desktop.
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
##################################### | |
jasmine_fixtures_test_helper.rb - Include in lib/ | |
##################################### | |
class ActionController::TestCase | |
def self.save_fixture(fixtures, &expectations) | |
fixtures.each do |(fixture_name, selector)| | |
should "save the content of the selector #{selector} to fixture file #{fixture_name}.html" do | |
selected_section_in_document = Nokogiri::HTML(@response.body).css(selector) | |
instance_exec(selected_section_in_document, &expectations) if block_given? | |
fixture_content = selected_section_in_document.to_html | |
fixture_file = File.expand_path(RAILS_ROOT + "/tmp/js_dom_fixtures/#{fixture_name}.html") | |
FileUtils.mkdir_p File.dirname(fixture_file) | |
File.open(fixture_file, "w") { |file| file.puts fixture_content } | |
Rails.logger.info "Saving jasmine fixture #{fixture_file} (#{fixture_content.size} bytes)" | |
end | |
end | |
end | |
end | |
##################################### | |
In functional tests to save a fixture | |
##################################### | |
save_fixture "fixture-name-goes-here" => "#selector_for_fixture_root_node" do |content| | |
# make assertions on what should be in the fixture | |
assert content.css("form").any? | |
end | |
##################################### | |
SpecHelper.js | |
##################################### | |
$(function() { | |
$.realAjax = $.ajax; | |
}); | |
beforeEach(function() { | |
$.realAjax = $.ajax; | |
spyOn($, "ajax"); | |
spyOn(window, "confirm"); | |
spyOn(window, "alert"); | |
$.fn.jqmShow = $.noop; | |
$.fn.jqmHide = $.noop; | |
$.fn.datepicker = $.noop; | |
$.fn.placeholder = $.noop; | |
$.fn.ajaxForm = $.noop; | |
this.addMatchers({ | |
hasElements: function() { | |
if(this.actual instanceof jQuery) { | |
return this.actual.length > 0; | |
} else { | |
throw "ELEMENT IS NOT A JQUERY OBJECT"; | |
} | |
}, | |
is: function(selector) { | |
if(this.actual instanceof jQuery) { | |
return this.actual.is(selector); | |
} else { | |
throw "ELEMENT IS NOT A JQUERY OBJECT"; | |
} | |
}, | |
hasClass: function(className) { | |
if(this.actual instanceof jQuery) { | |
return this.actual.hasClass(className); | |
} else { | |
throw "ELEMENT IS NOT A JQUERY OBJECT"; | |
} | |
} | |
}); | |
}); | |
afterEach(function() { | |
specDOM().html(''); | |
}); | |
var FIXTURE_CACHE = {}; | |
function loadFixture(fixtureName) { | |
var fixtureContent = getFixtureHtml(fixtureName); | |
$("#jasmine_content").html(fixtureContent).find("input[type=file]").each(function() { | |
$(this).replaceWith($(this).clone().attr("type", "text")); | |
}); | |
} | |
function getFixtureHtml(fixtureName) { | |
var fixtureContent; | |
if (fixtureName in FIXTURE_CACHE) { | |
fixtureContent = FIXTURE_CACHE[fixtureName]; | |
} else { | |
var fixtureUrl = "/__root__/tmp/js_dom_fixtures/" + fixtureName + ".html"; | |
fixtureContent = FIXTURE_CACHE[fixtureName] = $.realAjax({cache: false, async: false, url: fixtureUrl}).responseText; | |
} | |
if (fixtureContent.indexOf('File not found')!=-1) { | |
throw ("FixtureNotFoundError: " + fixtureContent); | |
} | |
return fixtureContent; | |
} | |
function specDOM(selector) { | |
if(selector) { | |
return $(selector, "#jasmine_content"); | |
} else { | |
return $("#jasmine_content"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment