🖥

How to make a class method stub in Rspec

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オープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-16

Discussion