【问题标题】:How do I prevent RSpec from using "Rspec.describe... " and "type: ..." in generated specs?如何防止 RSpec 在生成的规范中使用“Rspec.describe...”和“type: ...”?
【发布时间】:2014-11-18 07:08:56
【问题描述】:

自从升级到 RSpec 3 后,我生成的规范文件包含 RSpec.describe 而不仅仅是 describe,并明确包含类型,例如:type => :model。例如,这是刚刚为名为“Plan”的类生成的模型规范文件:

require 'rails_helper'

RSpec.describe Plan, :type => :model do
  pending "add some examples to (or delete) #{__FILE__}"
end

我希望它看起来像这样(注意第 3 行的更改):

require 'rails_helper'

describe Plan do
  pending "add some examples to (or delete) #{__FILE__}"
end

...“类型”调用似乎特别多余,因为我在 rails_helper 中有行 config.infer_spec_type_from_file_location!

如何让 RSpec 生成看起来像我的第二个示例的规范文件?我不想每次都手动编辑它。

【问题讨论】:

    标签: ruby-on-rails rspec rspec3 rails-generators


    【解决方案1】:

    如果您查看rspec-rails github repo 中的模板,您会发现没有通过配置变量执行此操作的选项。但是您应该能够通过在 lib/generators/rspec/model/templates/ 中创建一个名为 model_spec.rb 的文件来自定义它,以便为您的模型创建自定义生成器。正如您在上面的链接中看到的,它只是一个 erb 模板:

    require 'rails_helper'
    
    <% module_namespacing do -%>
    RSpec.describe <%= class_name %>, :type => :model do
      pending "add some examples to (or delete) #{__FILE__}"
    end
    <% end -%>
    

    您应该能够轻松地自定义 Rspec 使用这种方法定义的任何生成器。你甚至可以添加额外的东西。更多信息是here。希望这对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-16
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多