0
0

More than 3 years have passed since last update.

Railsでstubを使う

Last updated at Posted at 2020-04-21

よく使う構文をメモ


post = Post.new

# 引数なし
allow(post).to receive(:title).and_return('title')

# 引数あり
allow(post).to receive(:title).with(arg1, arg2).and_return('title')

# エラーを起こさせる
allow(post).to receive(:subtitle).and_raise(StandardError.new)

# クラスに対してインスタンスメソッドのスタブ化
allow_any_instance_of(Post).to receive(:title).and_return('title')

追記

  • allow句はit, before, letブロックの中で使用可能。

参考

開発
Rails tips: RSpecでシンプルなスタブを使う(翻訳)

0
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
0
0