RSpec 模拟具有嵌套属性的 class

RSpec mock a class with nested attributes

我是 Ruby 和 RSpec 的新手,我在尝试模拟 class:

中的属性链时遇到了问题
client.conversations.configuration.update(sid: user.sid)

我已经这样嘲笑客户了:

let(:client) { class_double(Service::ClientFactory) }

但是,这是我使用 receive 方法尝试过但失败的方法,该方法不起作用。

  allow(client).to receive(conversations.configuration.update).and_return("123123123")

我怎样才能正确地做到这一点? 谢谢!

您可能想使用 receive_message_chain:

allow(
  client
).to receive_message_chain(:conversations, :configuration, update: "123123123")

但也请查看该页面上的部分:

Warning:

Chains can be arbitrarily long, which makes it quite painless to violate the Law of Demeter in violent ways, so you should consider any use of receive_message_chain a code smell. [...]