我正在尝试使用 Bundler 和 Thor 编写一个小的 ruby gem 来生成一些模板。我正在用 Cucumber 和 Aruba 编写测试,但无法让它们通过。
我在应用程序中定义了以下 Thor CLI 类:
require 'thor'
require 'sleipnir'
require 'sleipnir/generators/layout'
require 'sleipnir/generators/app'
module Sleipnir
class CLI < Thor
desc "app", "Generates an app directory and copies the appropriate files"
def app(app_name)
Sleipnir::Generators::App.start([app_name])
end
desc "layout", "Generates specific layout based on template type"
def layout(template_type)
Sleipnir::Generators::Layout.start([template_type])
end
end
end
这是app.rb
文件:
require 'thor/group'
module Sleipnir
module Generators
class App < Thor::Group
include Thor::Actions
argument :app_name, :type => :string
class_option :template_type, :default => :erb, :required => true
def self.source_root
File.dirname(__FILE__)
end
def create_app_dir
empty_directory(app_name)
end
def copy_app_scaffold
directory("app", app_name)
end
end
end
end
和layout.rb
文件:
require 'thor/group'
module Sleipnir
module Generators
class Layout < Thor::Group
include Thor::Actions
class_option :template_type, :default => :erb, :required => true
def self.source_root
File.dirname(__FILE__) + "/template"
end
def copy_layout
template_type = options[:template_type]
template("layout_template.#{template_type}", "views/layout.#{template_type}")
end
end
end
end
我为该app
方法编写了一个黄瓜测试,它通过了。但是,该layout
方法失败了。这是测试:
Feature: Generate
In order to generate templates
As a CLI
I want to run the generator
Scenario: Layout
When I run `sleipnir app test_app`
Then the following directories should exist:
| test_app/views |
When I run `sleipnir layout --template_type "erb"`
Then the following files should exist:
| test_app/views/layout.erb |
测试的第一部分很好地通过了(即创建了目录),但是关于验证文件是否存在的部分失败了。我检查了文件结构,并且layout_template.erb
文件存在,所以我无法弄清楚为什么它没有正确模板化。