0

ハッシュテーブルの各キーに対して、リソースタイプインスタンスに属性を割り当てます。キーが存在しない場合は、適切なメッセージで失敗したいと思います。
rspecのコードを掘り下げた後、モジュールをミックスインすると、テストが失敗したことを報告する必要があるRSpec::Expectations呼び出しができることがわかりました。fail_with()ただし、「スタックレベルが深すぎます」SystemStackErrorという呼び出し行から取得しています。fail_with()私はそれを間違っていると思います。意味のあるメッセージでテストを失敗させる方法はありますか?

require 'yaml'

module Serverspec
  module Type

    class YAMLFile < Base
      include RSpec::Expectations

      def initialize(path)
        yaml = YAML.load_file(path)
        return unless yaml

        yaml.each { |key, value| self.send('#{key}=', value) }
      end

      def method_missing(m, *args, &block)
        # fail here with message
        self.fail_with('The key does not exist in the YAML file')
      end
    end

    def yaml_file(path)
      YAMLFile.new(path)
    end
  end
end

include Serverspec::Type
4

1 に答える 1

0

fail()解決策は、 の代わりに メソッドを使用することですfail_with()。使えない理由は未だにわかりませんfail_with()。将来の参照用の作業コードは次のとおりです。

require 'yaml'

module Serverspec
  module Type

    class YAMLFile < Base
      def initialize(path)
        yaml = YAML.load_file(path)
        return unless yaml

        yaml.each { |key, value| self.send('#{key}=', value) }
      end

      def method_missing(m, *args, &block)
        fail "The key '%s' does not exist in the YAML document" % m
      end
    end

    def yaml_file(path)
      YAMLFile.new(path)
    end
  end
end

include Serverspec::Type
于 2015-10-08T07:34:47.863 に答える