【发布时间】:2009-05-19 09:38:32
【问题描述】:
是否有任何命令可用于为现有模型/控制器生成所有缺失的规范文件?我有一个项目,其中有几个模型是在没有规范文件的情况下生成的。
【问题讨论】:
标签: ruby-on-rails ruby rspec specifications
是否有任何命令可用于为现有模型/控制器生成所有缺失的规范文件?我有一个项目,其中有几个模型是在没有规范文件的情况下生成的。
【问题讨论】:
标签: ruby-on-rails ruby rspec specifications
在用于 Rails 3 的 rspec-rails-2 中,所有 rspec 生成器都已被删除。
您可以通过运行 rails 模型生成器来解决这个问题。您可以添加 -s 跳过任何现有文件,添加 --migration=false 跳过创建迁移文件。
像这样:
rails generate model example -s --migration=false
【讨论】:
您可以只运行生成器并忽略模型/迁移/固定装置。
ruby script/generate rspec_model User --skip-migration --skip-fixture --skip
我一直在考虑写一些东西来做这件事,但其他人没有任何兴趣。
【讨论】:
如果缺少规范的数量相当少,您可以简单地为缺少规范的每个组件运行rails generate 命令。
发生冲突时,只需选择不覆盖原始文件即可。生成器将忽略现有文件并生成丢失的文件。
【讨论】:
https://gist.github.com/omenking/7774140
require 'fileutils'
namespace :spec do
def progress name, x, y
print "\r #{name}: #{x}/#{y} %6.2f%%" % [x.to_f/y * 100]
end
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'
end
count = collection.count
collection.each_with_index do |i,index|
`rails g #{kind} #{$1} -s` if i =~ /#{root}(.+)#{ext}/
progress name, index, count
end
end
task generate_missing: :environment do
generate_files :controllers
generate_files :models
end
end
# if you dont want certian things generated than
# configure your generators in your application.rb eg.
#
# config.generators do |g|
# g.orm :active_record
# g.template_engine :haml
# g.stylesheets false
# g.javascripts false
# g.test_framework :rspec,
# fixture: false,
# fixture_replacement: nil
# end
#
【讨论】: