0

我正在为属于同一命名空间的两个类创建间谍,目的是希望每个类都接收特定的参数:

allow(SibApiV3Sdk::SendSmtpEmail).to receive(:new).and_return(seb_send_email)
allow(SibApiV3Sdk::SMTPApi).to receive(:new).and_return(seb_smtp_api)

def seb_send_email
  @seb_smtp_api ||= SibApiV3Sdk::SendSmtpEmail.new(email_params)
end

def seb_smtp_api
  @seb_smtp_api ||= SibApiV3Sdk::SMTPApi.new
end

当我这样做时,第二个间谍无法正常工作并返回第一个间谍对象。我怀疑这与它是一个命名空间类有关。这是预期的行为吗?是否有处理命名空间类间谍的替代方法?

4

1 回答 1

2

You assign both to @seb_smtp_api variable and that's the source of your problems.

You probably call the seb_send_email method first, then it's memoized as @seb_smtp_api and when you call seb_smtp_api it just returns the memoized value.

You can check that by replacing allow with expect and see that SibApiV3Sdk::SMTPApi's new method is never called.

于 2018-07-15T20:59:27.870 回答