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?

More than 1 year has passed since last update.

How to make a class method stub in Rspec

Last updated at Posted at 2019-04-16

Any class method can be rewritten using allow.

Example) Definition of model

 class Example def self.class_method 'This is real class method' end end 

How to make a stub

  • Specify class name with allow.

  • Specify a method with receive.

  • Specify return value at method execution time with and_return.

    require 'spec_helper' describe do before do allow(Example).to receive(:class_method).and_return('This is stub class method') end it do p Example.class_method end end

result

"This is stub class method" is output.

environment

  • rspec-rails 2.14.1

Original by

Rspec で クラスメソッドのスタブを作る方法

About

About this translattion

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

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?