Rspec-core: 元数据中的多种规格

创建于 2016-10-24  ·  3评论  ·  资料来源: rspec/rspec-core

我想为元数据定义多种类型的规范,但目前不可能。

我想定义如下内容:

config.when_first_matching_example_defined(types: %i(feature request)) do
  # enable db support only for feature or request specs
  require 'support/db'
end

# include these Helpers only into feature or request specs
config.include(Helpers, types: %i(feature request))

目前我必须复制定义。

config.when_first_matching_example_defined(types: :feature) do
  require 'support/db'
end

config.when_first_matching_example_defined(types: :request) do
  require 'support/db'
end

config.include(Helpers, type: :feature)
config.include(Helpers, type: :request)

最有用的评论

我不认为上述解决方案提高了可读性和spec/rails_helper 。 一些单线解决方案非常适合这些情况。 我不认为,只有我一个人会欣赏我提出的建议。

所有3条评论

你可以这样做:

%i(feature request).each do |type|
  config.when_first_matching_example_defined(type: type) do
    # enable db support only for feature or request specs
    require 'support/db'
  end

  # include these Helpers only into feature or request specs
  config.include(Helpers, type: type)
end

鉴于使用简单循环非常容易,我认为我们没有理由为您所要求的内容添加支持 - 特别是因为这将是元数据匹配的一种非常隐含、令人困惑的方式,IMO .

我不认为上述解决方案提高了可读性和spec/rails_helper 。 一些单线解决方案非常适合这些情况。 我不认为,只有我一个人会欣赏我提出的建议。

鉴于目前我们将元数据值视为简单值,开始赋予它们特殊含义确实会令人困惑,更不用说模棱两可了!

此页面是否有帮助?
0 / 5 - 0 等级