【问题标题】:Cucumber/Aruba Thor: testing multiple commands in a single scenario, first passes and second failsCucumber/Aruba Thor:在单个场景中测试多个命令,第一次通过,第二次失败
【发布时间】:2013-01-16 22:19:35
【问题描述】:

我正在尝试使用 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 文件存在,所以我不知道为什么它没有正确模板化。

【问题讨论】:

    标签: ruby cucumber thor aruba


    【解决方案1】:

    很遗憾,Aruba 不支持此功能:https://github.com/cucumber/aruba/issues/140

    但是编写你自己的步骤定义也不错,只要在你传入的命令之前先执行你想要执行的操作即可。有关示例,请参见https://github.com/cucumber/cucumber/wiki/Step-Definitions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 2021-01-20
      • 2011-05-02
      • 2015-03-27
      • 2011-08-04
      • 2015-09-01
      • 1970-01-01
      • 2016-06-09
      相关资源
      最近更新 更多