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

rspec-mocks の stub/unstub を allow に置き換える

Posted at

やりたいこと

RSpec を使ったテストコードで stub/unstub という古いメソッドを使用している箇所があった。allow1に置き換えたい。

方法 1

厳密には unstub と互換性のある方法はない。and_call_original で元のメソッドを呼び出すようにすることで擬似的にスタブを解除する。

Hoge.stub(:fuga)
Hoge.unstub(:fuga)

allow(Hoge).to receive(:fuga)
allow(Hoge).to receive(:fuga).and_call_original

方法 2

以下の方法でスタブを解除できるが、公開されている方法ではない (undocumented) ので非推奨。

Hoge.stub(:fuga)
Hoge.unstub(:fuga)

allow(Hoge).to receive(:fuga)
RSpec::Mocks.space.proxy_for(Hoge).reset
  1. allow を使った記法は RSpec 2.14 で rspec-mocks に導入された。そして 2025/03/31 (月) 時点の RSpec の最新バージョンは 3.13.0。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?