1
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 5 years have passed since last update.

Rspecでmodelのinstance methodをstabとして上書きして、無効化する

Last updated at Posted at 2015-01-15

メモです。

例:テストでUserの何かのメソッドを無効化したいとき

app/model/user.rb
class User < ActiveRecord::Base
  def some_method
    #テストでは実行したくない処理
  end
end

allow_any_instance_of.to receiveを使います。

spec/model/user_spec.rb
  before do
    allow_any_instance_of(User)
      .to receive(:some_method)
  end

.any_instance.stubでも実現できますが、それだと警告(deprecation warning)が出るので、こちらの方が良さそうです。

参考

1
1
4

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