receive
熱心に失敗させる方法がわかりません。ただし、have_received
熱心に失敗するため、それがいくつかの期待の最初のものである場合、テストに失敗し、RSpec が報告するものになります。
class Foo
def self.bar(baz)
end
end
describe "RSpec" do
it "reports a non-mock expectation failure before a mock expectation failure" do
expect(Foo).to receive(:bar).with(1)
expect(true).to be_falsy # RSpec reports this failure
Foo.bar 2
end
it "reports a spy expectation failure when you'd expect it to be reported" do
allow(Foo).to receive(:bar) # Spy on Foo
Foo.bar 2
expect(Foo).to have_received(:bar).with(1) # RSpec reports this failure
expect(true).to be_falsy
end
end
詳細については、RSpec スパイのドキュメントを参照してください。
このソリューションは、次の場合に最適です。
- 私のように論理的なアレンジ-アクト-アサートシーケンスでテストしたい場合
- スパイの期待が失敗した場合、他の期待は気にしません
- ブロック
allow
よりもスパイを設定する余分な入力を気にする場合aggregate_failures
通常のモックと Adarsh のソリューションの方が優れています
- どちらかが失敗するかどうかに関係なく、両方の期待の結果を見たい
- スパイを設定する
aggregate_failures
余分な入力を気にするよりもブロックを気にする場合allow