任意のヘルパーメソッドを定義する
Defining arbitrary helper methods
define_method
メソッドまたはrubyの関数キーワードを使ってどのexmapleグループ内でもメソッドを定義できる。
それらのヘルパーメソッドはそのグループ内に入れ子されたグループと定義されたグループでexampleたちに公開される。しかし親グループまたは兄弟グループには公開されていません。
You can define methods in any example group using Ruby’s def keyword or define_method method. These helper methods are exposed to examples in the group in which they are defined and groups nested within that group, but not parent or sibling groups.
同じグループ内でメソッドを定義する
Use a method defined in the same group
RSpec.describe "an example" do
def help
:available #rubyの関数キーワードを使ってexmapleグループ内でメソッドを定義することでhelpメソッドが呼ばれる
end
it "has access to methods defined in its group" do
expect(help).to be(:available) # 同一の関数であるからtrue
end
end
Run options: include {:full_description=>/an\ example/}
1 example, 0 failures, 1 passed
親グループ内で定義されたメソッドを使う
Use a method defined in a parent group
RSpec.describe "an example" do # 親グループ
def help
:available # 親グループで定義する
end
describe "in a nested group" do # 子グループ、入れ子されたグループ
it "has access to methods defined in its parent group" do
expect(help).to be(:available) # 親グループで定義されたメソッドを使える
end
end
end
Run options: include {:full_description=>/an\ example/}
1 example, 0 failures, 1 passed