Skip to content

Instantly share code, notes, and snippets.

@bonkydog
Created February 3, 2012 05:18
Show Gist options
  • Save bonkydog/1728274 to your computer and use it in GitHub Desktop.
Save bonkydog/1728274 to your computer and use it in GitHub Desktop.
Generate Jasmine fixtures using RSpec
module JasmineFixtures
extend ActiveSupport::Concern
module InstanceMethods
# Saves the markup to a fixture file using the given name
def save_fixture(name)
response.should be_success
fixture_path = File.join(Rails.root, '/spec/javascripts/fixtures')
fixture_file = File.join(fixture_path, name)
fixture = case name
when /\.html$/
response.body.sub(/.*<body/im, '<div').sub(/<\/body>.*/im, "</div>")
when /\.json$/
JSON.pretty_generate(JSON.load(response.body))
else
response.body
end
File.open(fixture_file, 'w') { |file| file.write(fixture)}
end
end
included do
include RSpec::Rails::ControllerExampleGroup
include ControllerMacros
render_views
before do
DatabaseCleaner.clean_with(:truncation)
end
end
end
# Here's how you use it:
require 'spec_helper'
describe PeopleController do
include JasmineFixtures
login_user
describe "edit_person.html" do
it "is a person edit page" do
person = Factory(:person, :emails => [Email('bob@example.com'), Email('dobbsy@example.com')])
get :edit, :id => person.id
save_fixture('edit_person.html')
end
end
end
@walteryu
Copy link

walteryu commented Feb 3, 2012

Thanks for sharing, I will keep you posted on how our Jasmine test suite is coming along.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment