Rspec 他のメソッド呼ぶスペックの書き方
メモ
class A def call_other_method other_method1 end def other_method1 end def other_method2 end end describe A, "#call_other_methodを実行した場合" do before(:each) do @a = A.new end it "other_method1を呼ぶこと" do @a.should_receive(:other_method1).with(no_args) @a.call_other_method end it "other_method2を呼ぶこと" do @a.should_receive(:other_method2).with(no_args) @a.call_other_method end end
A#call_other_methodはA#other_method2を呼ぶため、2番目のitは失敗する。
他クラスのメソッドを呼ぶ場合
class A def call_other_class_method B.klass_method end end class B def self.klass_method end end describe A, "#call_other_class_methodを実行した場合" do it "B::klass_methodを呼ぶこと" do B.should_receive(:klass_method).with(no_args) a = A.new a.call_other_class_method end end
呼ぶことだけを確認できるって素晴らしい。
何故なら呼ばれたメソッドの責務を呼ぶ側が負わなくていい。
つまりはDRYなスペックが書けるということだ。