Created
February 3, 2012 05:18
-
-
Save bonkydog/1728274 to your computer and use it in GitHub Desktop.
Generate Jasmine fixtures using RSpec
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing, I will keep you posted on how our Jasmine test suite is coming along.