Skip to content

Instantly share code, notes, and snippets.

@rmw
Created December 7, 2012 21:39
Show Gist options
  • Select an option

  • Save rmw/4236750 to your computer and use it in GitHub Desktop.

Select an option

Save rmw/4236750 to your computer and use it in GitHub Desktop.
Generate fixtures in Rspec for Jasmine
rake spec:jasmine
rake spec:jasmine:ci
src_files:
- gem_vendored.js #need this for jasmine-headless
- assets/gem_vendored.js #this for regular jasmine
- spec/javascripts/support/vendor/**/*.js
- spec/javascripts/support/helpers/**/*.js
- app/assets/javascripts/head.js
- app/assets/javascripts/lib/**/*.js
- app/assets/javascripts/application/**/*.js
- app/assets/javascripts/views/**/*.js
module JasmineFixtures
# Saves the markup to a fixture file using the given name
def save_fixture(markup, name)
fixture_path = File.join(Rails.root, '/spec/javascripts/fixtures/views/')
Dir.mkdir(fixture_path) unless File.exists?(fixture_path)
fixture_file = File.join(fixture_path, "#{name}.fixture.html.erb")
File.open(fixture_file, 'w') do |file|
file.puts(markup)
end
end
# From the controller spec response body, extracts html identified
# by the css selector.
def html_for(selector)
html = defined?(rendered) ? rendered : response.body
doc = Nokogiri::HTML(html)
remove_third_party_scripts(doc)
content = doc.css(selector).first.to_s
return convert_body_tag_to_div(content)
end
# Remove scripts such as Google Analytics to avoid running them
# when we load into the dom during js specs.
def remove_third_party_scripts(doc)
scripts = doc.at('.third-party-scripts')
scripts.remove if scripts
end
# Many of our css and jQuery selectors rely on a class attribute we
# normally embed in the <body>. For example:
#
# <body class="workspaces show">
#
# Here we convert the body tag to a div so that we can load it into
# the document running js specs without embedding a <body> within a <body>.
def convert_body_tag_to_div(markup)
return inline_js(markup).gsub("<body", '<div').gsub("</body>", "</div>")
end
def inline_js(content)
doc = Nokogiri::HTML(content)
doc.xpath("//script").each do |node|
if script_file = node.get_attribute('src')
script_file_path = script_file.split('/')
script_file_path[0] = script_file_path[1]
script_file_path[1] = 'javascripts'
script_file_path.unshift('app')
filepath = "#{Rails.root}/#{script_file_path.join('/')}"
if File.exist?(filepath)
node.inner_html = File.read(filepath)
node.remove_attribute('src')
end
end
end
return doc.inner_html
end
end
def inline_js(content)
doc = Nokogiri::HTML(content)
doc.xpath("//script").each do |node|
if script_file = node.get_attribute('src')
script_file_path = script_file.split('/')
script_file_path[0] = script_file_path[1]
script_file_path[1] = 'javascripts'
script_file_path.unshift('app')
filepath = "#{Rails.root}/#{script_file_path.join('/')}"
if File.exist?(filepath)
node.inner_html = File.read(filepath)
node.remove_attribute('src')
end
end
end
return doc.inner_html
end
namespace :spec do
namespace :jasmine do
desc "run specs to generate js fixtures"
task :views do
`rm spec/javascripts/fixtures/views/*`
puts `rspec spec -e 'js fixtures'`
end
desc "run specs to generate js fixtures then run jasmine"
task :server do
Rake::Task["spec:jasmine:views"].invoke
Rake::Task["jasmine"].invoke
end
desc "run specs to generate js fixtures then run jasmine ci"
task :ci do
Rake::Task["spec:jasmine:views"].invoke
Rake::Task["jasmine:ci"].invoke
end
end
desc "Run specs to generate js fixtures then run jasmine"
task :jasmine => ['jasmine:server']
end
myCompany.test.loadView = function(name) {
loadFixtures('views/' + name + '.fixture.html.erb');
}
describe "js fixtures" do
render_views
it "generates users/new" do
get :new, token: invitation.code
response.should be_success
save_fixture(html_for('body'), 'users_new')
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment