代码之家  ›  专栏  ›  技术社区  ›  Codebeef

为RSpec生成缺少的规范文件

  •  16
  • Codebeef  · 技术社区  · 17 年前

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

    4 回复  |  直到 17 年前
        1
  •  17
  •   Benry    15 年前

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

    像这样:

    rails generate model example -s --migration=false
    
        2
  •  7
  •   Brian Hogan    17 年前

    您可以运行生成器,忽略模型/迁移/装置。

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

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

        3
  •  1
  •   mc9 eclipse1203    11 年前

    rails generate 用于缺少规格的每个零部件的命令。

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

        4
  •  0
  •   Andrew WC Brown    13 年前

    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
    #
    
    推荐文章