【问题标题】:Rake stats and Cucumber耙统计和黄瓜
【发布时间】:2009-10-23 13:59:38
【问题描述】:

我在 Rails 项目中使用 Cucumber 和 RSpec。 当我使用“rake stats”任务时,我得到以下信息:

+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |   948 |   761 |      21 |      87 |   4 |     6 |
| Helpers              |   212 |   175 |       0 |      24 |   0 |     5 |
| Models               |   912 |   741 |      28 |      72 |   2 |     8 |
| Libraries            |   305 |   211 |       1 |      25 |  25 |     6 |
| Model specs          |   978 |   797 |       0 |       1 |   0 |   795 |
| View specs           |   270 |   227 |       0 |       0 |   0 |     0 |
| Controller specs     |  1144 |   944 |       0 |       4 |   0 |   234 |
| Helper specs         |   154 |   116 |       0 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                |  4923 |  3972 |      50 |     213 |   4 |    16 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 1888     Test LOC: 2084     Code to Test Ratio: 1:1.1

因此 rspec 添加了它的统计信息(请参阅模型、视图、控制器和辅助规范)。
但我没有黄瓜的。我怎样才能将它们添加到我的 rake stats 中?

【问题讨论】:

    标签: ruby-on-rails rspec cucumber


    【解决方案1】:

    RSpec 创建一个lib/tasks/rspec.rake 文件。并重新定义了里面的stats目录。

    在该文件的第 108 行,您会看到:

    # Setup specs for stats
    task :statsetup do
        require 'code_statistics'
        ::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models')
        ::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views')
        ....
    end
    

    您只需要在任务结束之前将您的黄瓜功能目录添加到那里。

    #
    # Adding Cucumber features to the stats
    #
    ::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
    ::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')
    

    【讨论】:

    • 哇,真快!我正在检查。
    • 如果您不使用 rspec,有什么提示吗?
    • 您能告诉我们最新的 rspec-2 gem 是如何工作的吗?因为没有rake任务
    【解决方案2】:

    为什么猴子要那样修补宝石?每次更新 rspec-rails 时,您都必须为其添加修复程序。只需在本地扩展任务,如下所示:

    echo "# Adding Cucumber features to be reported by the command:
    # rake stats
    # But e.g. in production environment we probably don't have rspec-rails, nor it's
    # statsetup task, we could extend. So lets check if stasetup is available and only
    # then extend it. If it isn't then just do nothing.
    if Rake::Task.task_defined? 'spec:statsetup'
      Rake::Task['spec:statsetup'].enhance do
        require 'rails/code_statistics'
        ::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
        ::CodeStatistics::TEST_TYPES << 'Cucumber features' if File.exist?('features')
      end
    end" > lib/tasks/cucumber_stats.rake
    

    仅供参考,Lichtamberg 和 Damien MATHIEU 这适用于 rspec 2。但是,这些特定于 rails 的 rake 任务不是 rspec gem 本身的一部分,而是 rspec-rails gem 的一部分。

    附:这一切都使用 ruby​​ 1.9.2p180(2011-02-18 修订版 30909)[i686-linux]、rails 3.0.8、rspec 2.6.0 和 rspec-rails 2.6.1 进行了测试,它可能适用于旧版本的他们。

    【讨论】:

      【解决方案3】:

      这是一个适用于 Turnip 和 RSpec 2 的版本

      https://gist.github.com/2360892

      【讨论】:

        【解决方案4】:
        I copy paste my custom set up for RSpec 1:
        
        +----------------------+-------+-------+---------+---------+-----+-------+
        | Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
        +----------------------+-------+-------+---------+---------+-----+-------+
        | Controllers          |  1110 |   791 |      39 |      92 |   2 |     6 |
        | Helpers              |   449 |   373 |       0 |      39 |   0 |     7 |
        | Models               |  1986 |  1338 |      25 |     111 |   4 |    10 |
        | Libraries            |   652 |   479 |      11 |      41 |   3 |     9 |
        | Views                |  2911 |  2730 |       0 |       0 |   0 |     0 |
        | Static CSS           |  2776 |  2174 |       0 |       0 |   0 |     0 |
        | App javascript       |    16 |    14 |       0 |       0 |   0 |     0 |
        | Model specs          |   517 |   289 |       0 |       2 |   0 |   142 |
        | Helper specs         |    27 |    22 |       0 |       0 |   0 |     0 |
        | Library specs        |    31 |    24 |       0 |       0 |   0 |     0 |
        | Cucumber features    |   907 |   657 |       3 |      13 |   4 |    48 |
        +----------------------+-------+-------+---------+---------+-----+-------+
        | Total                | 11382 |  8891 |      78 |     298 |   3 |    27 |
        +----------------------+-------+-------+---------+---------+-----+-------+
          Code LOC: 7899     Test LOC: 992     Code to Test Ratio: 1:0.1
        

        代码:

        # here is my set up
            task :statsetup do
              require 'code_statistics'
        
              class CodeStatistics
                alias calculate_statistics_orig calculate_statistics
        
                def calculate_statistics
                  @pairs.inject({}) do |stats, pair|
                    if 3 == pair.size
                      stats[pair.first] = calculate_directory_statistics(pair[1], pair[2]); stats
                    else
                      stats[pair.first] = calculate_directory_statistics(pair.last); stats
                    end
                  end
                end
              end
              # http://www.pervasivecode.com/blog/2007/06/28/hacking-rakestats-to-get-gross-loc/
              ::STATS_DIRECTORIES << ['Views', 'app/views', /\.(rhtml|erb|rb)$/]
              ::STATS_DIRECTORIES << ['Static CSS',  'public/stylesheets', /\.css$/]
              ::STATS_DIRECTORIES << ['App javascript',  'public/javascripts', /application.js$/]
        
              # RSpec default
              ::STATS_DIRECTORIES << %w( Model\ specs spec/models ) if File.exist?('spec/models')
              ::STATS_DIRECTORIES << %w( View\ specs spec/views ) if File.exist?('spec/views')
              ::STATS_DIRECTORIES << %w( Controller\ specs spec/controllers ) if File.exist?('spec/controllers')
              ::STATS_DIRECTORIES << %w( Helper\ specs spec/helpers ) if File.exist?('spec/helpers')
              ::STATS_DIRECTORIES << %w( Library\ specs spec/lib ) if File.exist?('spec/lib')
              ::STATS_DIRECTORIES << %w( Routing\ specs spec/routing ) if File.exist?('spec/routing')
              ::STATS_DIRECTORIES << %w( Integration\ specs spec/integration ) if File.exist?('spec/integration')
              ::CodeStatistics::TEST_TYPES << "Model specs" if File.exist?('spec/models')
              ::CodeStatistics::TEST_TYPES << "View specs" if File.exist?('spec/views')
              ::CodeStatistics::TEST_TYPES << "Controller specs" if File.exist?('spec/controllers')
              ::CodeStatistics::TEST_TYPES << "Helper specs" if File.exist?('spec/helpers')
              ::CodeStatistics::TEST_TYPES << "Library specs" if File.exist?('spec/lib')
              ::CodeStatistics::TEST_TYPES << "Routing specs" if File.exist?('spec/routing')
              ::CodeStatistics::TEST_TYPES << "Integration specs" if File.exist?('spec/integration')
        
              # Cuke
              ::STATS_DIRECTORIES << %w( Cucumber\ features features ) if File.exist?('features')
              ::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')
        
            end
        

        【讨论】:

          【解决方案5】:

          虽然这是一个非常古老的问题,但在谷歌搜索“rails rake stats cucumber”时仍然是第一个问题,并且答案具有误导性或过时。

          对于未来的谷歌人,你需要做的就是运行

          $ rails generate cucumber:install
          

          这将创建必要的文件,特别是 lib/tasks/cucumber.rake 文件,它将“Cucumber Features”添加到 rake stats 输出。

          【讨论】:

            猜你喜欢
            • 2011-09-15
            • 2013-10-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-01-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多