防止自定义生成器生成测试
Prevent custom generator from generating tests
我正在学习编写生成器。我使用 Rails' scaffold_controller 生成器作为起点。
require 'rails/generators/resource_helpers'
module Rails
module Generators
class ScaffoldControllerGenerator < NamedBase # :nodoc:
include ResourceHelpers
check_class_collision suffix: "Controller"
class_option :helper, type: :boolean
class_option :orm, banner: "NAME", type: :string, required: true,
desc: "ORM to generate the controller for"
argument :attributes, type: :array, default: [], banner: "field:type field:type"
def create_controller_files
template "controller.rb", File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
end
#hook_for :template_engine, :test_framework, as: :scaffold
end
end
end
如您所见,我注释掉了 hook_for
,希望这不会连接测试框架生成器。但是,当我 运行 这个生成器时,测试仍然会生成。另外,我还想跳过 jbuilder 和 helper 生成。这是生成的所有事物的列表:
create app/controllers/books_controller.rb
invoke erb
create app/views/books
create app/views/books/index.html.erb
create app/views/books/edit.html.erb
create app/views/books/show.html.erb
create app/views/books/new.html.erb
create app/views/books/_form.html.erb
invoke test_unit
create test/controllers/books_controller_test.rb
invoke helper
create app/helpers/books_helper.rb
invoke test_unit
invoke jbuilder
create app/views/books/index.json.jbuilder
create app/views/books/show.json.jbuilder
如何通过配置自定义生成器来生成 test/helper/jbuilder?
我意识到我实际上是猴子修补同一个生成器class,所以原来的设置仍然完好无损。
我必须手动删除这些挂钩。
remove_hook_for :jbuilder, :test_framework
我正在学习编写生成器。我使用 Rails' scaffold_controller 生成器作为起点。
require 'rails/generators/resource_helpers'
module Rails
module Generators
class ScaffoldControllerGenerator < NamedBase # :nodoc:
include ResourceHelpers
check_class_collision suffix: "Controller"
class_option :helper, type: :boolean
class_option :orm, banner: "NAME", type: :string, required: true,
desc: "ORM to generate the controller for"
argument :attributes, type: :array, default: [], banner: "field:type field:type"
def create_controller_files
template "controller.rb", File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
end
#hook_for :template_engine, :test_framework, as: :scaffold
end
end
end
如您所见,我注释掉了 hook_for
,希望这不会连接测试框架生成器。但是,当我 运行 这个生成器时,测试仍然会生成。另外,我还想跳过 jbuilder 和 helper 生成。这是生成的所有事物的列表:
create app/controllers/books_controller.rb
invoke erb
create app/views/books
create app/views/books/index.html.erb
create app/views/books/edit.html.erb
create app/views/books/show.html.erb
create app/views/books/new.html.erb
create app/views/books/_form.html.erb
invoke test_unit
create test/controllers/books_controller_test.rb
invoke helper
create app/helpers/books_helper.rb
invoke test_unit
invoke jbuilder
create app/views/books/index.json.jbuilder
create app/views/books/show.json.jbuilder
如何通过配置自定义生成器来生成 test/helper/jbuilder?
我意识到我实际上是猴子修补同一个生成器class,所以原来的设置仍然完好无损。
我必须手动删除这些挂钩。
remove_hook_for :jbuilder, :test_framework