モジュール内のヘルパーメソッドを定義する
Define helper methods in a module
モジュール内でヘルパーメソッドを定義し、config.include
構成オプションを使ってexampleグループ内でそれをインクルードできる。config.extend
はモジュールをexampleグループに拡張するために使われるのでモジュール内のメソッドがexampleグループ自体で利用可能になる(しかし実際のexampleでは利用できない)
You can define helper methods in a module and include it in your example groups using the config.include configuration option. config.extend can be used to extend the module onto your example groups so that the methods in the module are available in the example groups themselves (but not in the actual examples).
あなたはモジュールを最後の引数としてメタデータハッシュを渡すことによって特定のexampleグループのみへインクルードまたはエクステンドもできる。
与えられたメタデータと一致したのグループのみがモジュールをインクルードまたはエクステンドするだろう。シンボルのみを使ってメタデータを明示することもできる。
You can also include or extend the module onto only certain example groups by passing a metadata hash as the last argument. Only groups that match the given metadata will include or extend the module. You can also specify metadata using only symbols.
config.includeモジュールのメタデータと一致するexampleはモジュールもインクルードされていることに注意してください
Rspecはすべてのexampleをただ一つのexampleを含むシングルトンexampleグループ(rubyのシングルトングループに類似ている)を持つものとして取り扱う。
Note that examples that match a config.include module’s metadata will also have the module included. RSpec treats every example as having a singleton example group (analogous to Ruby’s singleton classes) containing just the one example.
Background
Given a file named “helpers.rb” with:
module Helpers # 呼び出されるモジュール名
def help
:available
end
end
すべてのexampleグループ内でモジュールをインクルードする
Include a module in all example groups
require './helpers' # 別ファイルのファイルにあるモジュールをインクルード
RSpec.configure do |c| # config.include 構成オプションをオプションを使っている
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
Run options: include {:full_description=>/an\ example\ group/}
1 example, 0 failures, 1 passed
すべてのexampleグループ内でモジュールをエクステンドする
Extend a module in all example groups
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
Help is available
Run options: include {:full_description=>/an\ example\ group/}
1 example, 0 failures, 1 passed
require 'shared_stuff'
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
Help is available
Run options: include {:full_description=>/an\ example\ group/}
You must pass an argument rather than a block to `expect` to use the provided matcher (eq 1), or the matcher must implement `supports_block_expectations?`.
0) an example group does not have access to the helper methods defined in the module
Failure/Error: expect { help }.to eq(1)
You must pass an argument rather than a block to `expect` to use the provided matcher (eq 1), or the matcher must implement `supports_block_expectations?`.
...
1 example, 1 failure, 0 passed
raise_error エラーが起こるかどうか確かめるマッチャー
extend モジュールのメソッドをそのクラスの特異メソッドにできる