diary

I like Hatena Star with a text selection.

2021-01-12

RSpecで次のようなコードが書かれていてハマった。

expect(something).to receive_message_chain(:foo, :bar).with(1).with(2)
something.foo(1).bar(2)

これはfoo(1).bar(2)が呼ばれることを期待していて、テストは通るのだけど、実際にはwith(1)は無視される。そのため、fooには何を渡してもテストが通る。

関連して https://github.com/rspec/rspec-mocks/issues/921 を教えてもらった。

but it looks like we added support for with to #697. We did that for stub_chain parity, but it's actually a bit confusing -- with specifies the expected arguments for one method call but receive_message_chain is about a chain of method calls so there's a bit of a semantic mis-match.

とこのIssueで言っていて完全に正しいという気持ち。