3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【RSpec】let にパラメータを渡したい

Posted at

はじめに

letにパラメータ(引数)を渡す方法です。
通常であればヘルパーメソッドで定義したり変数の部分をletで置いたりするなどと思いますが、選択肢の一つになれば幸いです。

やりかた

lambdaで渡す(Procでも可)。

# 定義の仕方
let(メソッド名) do
  ->(引数) { 処理 }
end

# 呼び出しかた
メソッド名.call(引数)

例題

# 定義
let(:say_something) do
  ->(sth) { p sth }
end

# 呼び出し
before do
  say_something.call('hogehoge')
  say_something.call('piyo')
end

おわりに

意外と知らなかったところだったので共有でした。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?