Skip to content

Instantly share code, notes, and snippets.

@12spokes
Created December 2, 2010 17:36
Show Gist options
  • Save 12spokes/725723 to your computer and use it in GitHub Desktop.
Save 12spokes/725723 to your computer and use it in GitHub Desktop.
Create jasmine fixtures from your view specs in RSpec 2.
# More than inspired by JB Steadman - http://pivotallabs.com/users/jb/blog/articles/1152-javascripttests-bind-reality-
#
# Adds a save_fixture method to Rspec 2's ViewExampleGroups which takes the
# rendered and stores it in a js_dom fixture file to be used with jasmine.
#
# Simply drop this into spec/support.
#
# Then, at the end of a view spec that you want to save as a fixture, simply call
# save_fixture('name_of_fixture_file')
#
# We're loading fixtures with the support of the jasmine-jquery plugin, which
# should be configured to look at the new fixture_path like so:
#
# In spec/javascripts/helpers/jasmine-fixtures-helper.js
# jasmine.getFixtures().fixturesPath = 'tmp/js_dom_fixtures';
#
# Once that's done, run your specs, load your fixtures and jasmine power!
RSpec::Rails::ViewExampleGroup::InstanceMethods.class_eval do
# Saves the rendered as a fixture file.
def save_fixture(name)
fixture_path = File.join(Rails.root, '/tmp/js_dom_fixtures')
Dir.mkdir(fixture_path) unless File.exists?(fixture_path)
fixture_file = File.join(fixture_path, "#{name}.html")
File.open(fixture_file, 'w') do |file|
file.puts(rendered)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment