Skip to content

Instantly share code, notes, and snippets.

@webervin
Forked from tcocca/missing_spec_generator.rb
Created October 5, 2011 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webervin/1263960 to your computer and use it in GitHub Desktop.
Save webervin/1263960 to your computer and use it in GitHub Desktop.
Generates missing specs and factories. NB assumes you have GenericSpecForModels module defined somewhere (just drop in lib/tasks)
class MissingSpecGenerator
#based on https://gist.github.com/537795 by tcoccara
def spec_file(spec_path, file_name, spec_template, namespace)
spec_name = file_name.gsub('.rb', '') + '_spec.rb'
if File.exist?("#{spec_path}/#{spec_name}")
puts "#{spec_path}/#{spec_name} exists"
else
puts "#{spec_path}/#{spec_name} missing"
puts "\n"
spec_file = ERB.new(spec_template)
class_name = "#{namespace}#{file_name.gsub('.rb', '').camelcase}"
spec = spec_file.result(binding)
puts spec
FileUtils.mkdir_p(spec_path) unless File.exist?(spec_path)
File.open("#{spec_path}/#{spec_name}", 'w') {|f| f.write(spec) }
puts "\n"
end
end
def factory_file(file_name, template, namespace)
spec_path = ::Rails.root.to_s + '/spec/factories/'
spec_name = file_name.gsub('.rb', '') + '_factory.rb'
if File.exist?("#{spec_path}/#{spec_name}")
puts "#{spec_path}/#{spec_name} exists"
else
puts "#{spec_path}/#{spec_name} missing"
puts "\n"
spec_file = ERB.new(template)
factory_name = file_name.gsub('.rb', '')
spec = spec_file.result(binding)
puts spec
FileUtils.mkdir_p(spec_path) unless File.exist?(spec_path)
File.open("#{spec_path}/#{spec_name}", 'w') {|f| f.write(spec) }
puts "\n"
end
end
def traverse_models(path, spec_template, namespace = '')
Dir.open( ::Rails.root.to_s + '/app/' + path).each do |file_name|
unless file_name.match(/^\./) #skip hidden folders (.svn)
if File.directory?( ::Rails.root.to_s + '/app/' + path + '/' + file_name)
traverse_models("#{path}/#{file_name}", spec_template, "#{namespace}#{file_name.camelcase}::")
else
factory_file(file_name, spec_template, namespace)
end
end
end
end
def traverse_specs(path, spec_template, namespace = '')
Dir.open( ::Rails.root.to_s + '/app/' + path).each do |file_name|
unless file_name.match(/^\./) #skip hidden folders (.svn)
if File.directory?( ::Rails.root.to_s + '/app/' + path + '/' + file_name)
traverse_specs("#{path}/#{file_name}", spec_template, "#{namespace}#{file_name.camelcase}::")
else
spec_file("#{ ::Rails.root.to_s}/spec/#{path}", file_name, spec_template, namespace)
end
end
end
end
def generate_missing_helper_specs
helper_template = %q{require 'spec_helper'
describe <%= class_name %> do
#Delete this example and add some real ones or delete this file
it "should be included in the object returned by #helper" do
included_modules = (class << helper; self; end).send :included_modules
included_modules.should include(<%= class_name %>Helper)
end
end
}.gsub(/^ /, '')
traverse_specs('helpers', helper_template)
end
def generate_missing_controller_specs
controller_template = %q{require 'spec_helper'
describe <%= class_name %> do
#Delete this example and add some real ones
it "should use <%= class_name %>" do
controller.should be_an_instance_of(<%= class_name %>)
end
end
}.gsub(/^ /, '')
traverse_specs('controllers', controller_template)
end
def generate_missing_model_specs
model_template = %q{require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe <%= class_name %> do
include GenericSpecForModels
end
}.gsub(/^ /, '')
traverse_specs('models', model_template)
end
def generate_missing_factories
factory_template = %q{Factory.define :<%= factory_name %> do |factory|
end
}.gsub(/^ /, '')
traverse_models('models', factory_template)
end
end
namespace :spec do
desc "Generate the missing rspec files, and factory girl factories using inline template"
task :generate_missing => :environment do
msg = MissingSpecGenerator.new()
msg.generate_missing_helper_specs
msg.generate_missing_controller_specs
msg.generate_missing_model_specs
msg.generate_missing_factories
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment