在模块中定义辅助方法
您可以在模块中定义辅助方法,并使用 `config.include` 配置选项将其包含在示例组中。 `config.extend` 可用于将模块扩展到示例组,以便模块中的方法在示例组本身(但不在实际示例中)可用。
您也可以通过将元数据哈希作为最后一个参数传递,将模块仅包含或扩展到某些示例组。只有与给定元数据匹配的组才会包含或扩展模块。您也可以仅使用符号指定元数据。
请注意,与 `config.include` 模块的元数据匹配的示例也将包含该模块。RSpec 将每个示例视为拥有一个包含单个示例的单例示例组(类似于 Ruby 的单例类)。
背景
给定一个名为“helpers.rb”的文件,其中包含
module Helpers
def help
:available
end
end
在所有示例组中包含一个模块
给定一个名为“include_module_spec.rb”的文件,其中包含
require './helpers'
RSpec.configure do |c|
c.include Helpers
end
RSpec.describe "an example group" do
it "has access to the helper methods defined in the module" do
expect(help).to be(:available)
end
end
当我运行 `rspec include_module_spec.rb`
那么示例应该全部通过。
在所有示例组中扩展一个模块
给定一个名为“extend_module_spec.rb”的文件,其中包含
require './helpers'
RSpec.configure do |c|
c.extend Helpers
end
RSpec.describe "an example group" do
puts "Help is #{help}"
it "does not have access to the helper methods defined in the module" do
expect { help }.to raise_error(NameError)
end
end
当我运行 `rspec extend_module_spec.rb`
那么示例应该全部通过
并且输出应包含“帮助可用”。
仅在某些示例组中包含一个模块
给定一个名为“include_module_in_some_groups_spec.rb”的文件,其中包含
require './helpers'
RSpec.configure do |c|
c.include Helpers, :foo => :bar
end
RSpec.describe "an example group with matching metadata", :foo => :bar do
it "has access to the helper methods defined in the module" do
expect(help).to be(:available)
end
end
RSpec.describe "an example group without matching metadata" do
it "does not have access to the helper methods defined in the module" do
expect { help }.to raise_error(NameError)
end
it "does have access when the example has matching metadata", :foo => :bar do
expect(help).to be(:available)
end
end
当我运行 `rspec include_module_in_some_groups_spec.rb`
那么示例应该全部通过。
仅在某些示例组中扩展一个模块
给定一个名为“extend_module_in_only_some_groups_spec.rb”的文件,其中包含
require './helpers'
RSpec.configure do |c|
c.extend Helpers, :foo => :bar
end
RSpec.describe "an example group with matching metadata", :foo => :bar do
puts "In a matching group, help is #{help}"
it "does not have access to the helper methods defined in the module" do
expect { help }.to raise_error(NameError)
end
end
RSpec.describe "an example group without matching metadata" do
puts "In a non-matching group, help is #{help rescue 'not available'}"
it "does not have access to the helper methods defined in the module" do
expect { help }.to raise_error(NameError)
end
end
当我运行 `rspec extend_module_in_only_some_groups_spec.rb`
那么示例应该全部通过
并且输出应包含“在匹配的组中,帮助可用”
并且输出应包含“在不匹配的组中,帮助不可用”。
使用符号作为元数据
给定一个名为“symbols_as_metadata_spec.rb”的文件,其中包含
require './helpers'
RSpec.configure do |c|
c.include Helpers, :include_helpers
c.extend Helpers, :extend_helpers
end
RSpec.describe "an example group with matching include metadata", :include_helpers do
puts "In a group not matching the extend filter, help is #{help rescue 'not available'}"
it "has access to the helper methods defined in the module" do
expect(help).to be(:available)
end
end
RSpec.describe "an example group with matching extend metadata", :extend_helpers do
puts "In a group matching the extend filter, help is #{help}"
it "does not have access to the helper methods defined in the module" do
expect { help }.to raise_error(NameError)
end
end
当我运行 `rspec symbols_as_metadata_spec.rb`
那么示例应该全部通过
并且输出应包含“在与扩展过滤器不匹配的组中,帮助不可用”
并且输出应包含“在与扩展过滤器匹配的组中,帮助可用”。