在 Rails 3 中自定义 Rspec 生成器

发布于 2024-12-13 05:59:02 字数 651 浏览 0 评论 0原文

我正在编写一个 Rails 3.1 引擎并使用 RSpec 2 进行测试。当我使用 railsgenerate 时,我会自动为我生成规范文件,这非常方便:

$ rails g model Foo
  invoke  active_record
  create    db/migrate/20111102042931_create_myengine_foos.rb
  create    app/models/myengine/foo.rb
  invoke    rspec
  create      spec/models/myengine/foo_spec.rb

但是,为了使生成的规范能够很好地发挥作用使用我的独立命名空间,我每次都必须将规范手动包装在模块中:

require 'spec_helper'

module MyEngine
  describe Foo do
    it "should be round"
    ...
  end
end

我很想知道是否有一种干净且简单的方法来修改自动生成的规范“模板”,这样我就不必包装规范在每次生成新模型或控制器时,Module MyEngine

I'm writing a Rails 3.1 engine and testing with RSpec 2. When I use rails generate, I get spec files generated for me automatically, which is so convenient:

$ rails g model Foo
  invoke  active_record
  create    db/migrate/20111102042931_create_myengine_foos.rb
  create    app/models/myengine/foo.rb
  invoke    rspec
  create      spec/models/myengine/foo_spec.rb

However, to make the generated specs play nicely with my isolated namespace, I have to wrap the spec each time manually in a module:

require 'spec_helper'

module MyEngine
  describe Foo do
    it "should be round"
    ...
  end
end

I would love to know if there's a clean and easy way to modify the automatically generated spec 'templates' so that I don't have to wrap the spec in Module MyEngine each time I generate a new model or controller.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪推晚风 2024-12-20 05:59:02

您可以使用 Rake 任务复制 RSpec 的模板,如下所示:

namespace :spec do
  namespace :templates do
    # desc "Copy all the templates from rspec to the application directory for customization. Already existing local copies will be overwritten"
    task :copy do
      generators_lib = File.join(Gem.loaded_specs["rspec-rails"].full_gem_path, "lib/generators")
      project_templates = "#{Rails.root}/lib/templates"

      default_templates = { "rspec" => %w{controller helper integration mailer model observer scaffold view} }

      default_templates.each do |type, names|
        local_template_type_dir = File.join(project_templates, type)
        FileUtils.mkdir_p local_template_type_dir

        names.each do |name|
          dst_name = File.join(local_template_type_dir, name)
          src_name = File.join(generators_lib, type, name, "templates")
          FileUtils.cp_r src_name, dst_name
        end
      end
    end
  end
end

然后您可以修改 #{Rails.root}/lib/templates/rspec/model/model_spec.rb 中的代码以包含模块名称。

You can copy RSpec's templates using a Rake task like:

namespace :spec do
  namespace :templates do
    # desc "Copy all the templates from rspec to the application directory for customization. Already existing local copies will be overwritten"
    task :copy do
      generators_lib = File.join(Gem.loaded_specs["rspec-rails"].full_gem_path, "lib/generators")
      project_templates = "#{Rails.root}/lib/templates"

      default_templates = { "rspec" => %w{controller helper integration mailer model observer scaffold view} }

      default_templates.each do |type, names|
        local_template_type_dir = File.join(project_templates, type)
        FileUtils.mkdir_p local_template_type_dir

        names.each do |name|
          dst_name = File.join(local_template_type_dir, name)
          src_name = File.join(generators_lib, type, name, "templates")
          FileUtils.cp_r src_name, dst_name
        end
      end
    end
  end
end

Then you can modify the code in #{Rails.root}/lib/templates/rspec/model/model_spec.rb to include the module name.

燃情 2024-12-20 05:59:02

将“/lib/generators/rspec/scaffold/templates/controller_spec.rb”文件从 rspec-rails gem 复制到“./lib/templates/rspec/scaffold”文件夹,然后对其进行自定义。显然,如果您迁移到新版本的 rspec-rails,您将需要确保您的自定义模板没有过时。

Copy the '/lib/generators/rspec/scaffold/templates/controller_spec.rb' file from the rspec-rails gem to your './lib/templates/rspec/scaffold' folder, then customize it. Obviously, if you move to a new version of rspec-rails you will want to make sure that your customized template hasn't gotten stale.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文