Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Generate missing test files in a rails app
namespace :missing_tests do
def progress(name, x, y)
print "\r #{name}: #{x}/#{y} %6.2f%%" % [x.to_f / y * 100]
def generate_files(name)
kind = name.to_s.singularize
collection = Dir.glob Rails.root.join('app', name.to_s, '**', '*').to_s
root = Rails.root.join('app', name.to_s).to_s << '/'
ext = case name
when :controllers then '_controller.rb'
when :models then '.rb'
when :jobs then '_job.rb'
when :mailers then '_mailer.rb'
count = collection.count
collection.each_with_index do |filename, index|
`rails g test_unit:#{kind} #{Regexp.last_match(1)} -s` if filename.match(/#{root}(.+)#{ext}/)
progress name, index, count
task generate_tests: :environment do
generate_files :controllers
generate_files :models
generate_files :jobs
generate_files :mailers

This comment has been minimized.

Copy link
Owner Author

@lcpriest lcpriest commented May 16, 2018

Usually I will create files in Rails using generators, but sometimes I forget. I use this script to add missing test files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.