16

是否有任何命令可用于为现有模型/控制器生成所有缺少的规范文件?我有一个项目,其中有几个模型,这些模型是在没有规范文件的情况下生成的。

4

4 回答 4

17

在用于 Rails 3 的 rspec-rails-2 中,所有 rspec 生成器都已被删除。

您可以通过运行 rails 模型生成器来解决此问题。您可以添加 -s 以跳过任何现有文件,并添加 --migration=false 以跳过创建迁移文件。

像这样:

rails generate model example -s --migration=false
于 2011-06-03T06:58:16.867 回答
7

您可以只运行生成器并忽略模型/迁移/夹具。

ruby script/generate rspec_model User --skip-migration --skip-fixture --skip

我一直在考虑写一些东西来做到这一点,但其他人没有任何兴趣。

于 2009-05-19T12:16:42.383 回答
1

如果缺少规范的数量相当少,您可以简单地rails generate为缺少规范的每个组件运行命令。

当发生冲突时,只需选择不覆盖原始文件。生成器将忽略现有文件并生成丢失的文件。

于 2015-02-10T00:38:14.253 回答
0

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
#
于 2013-12-03T18:01:28.923 回答