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
About
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。