1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

任意のヘルパーメソッドを定義する

Posted at

任意のヘルパーメソッドを定義する

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

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?