简体   繁体   English

尝试在Rspec 3中为模型存根链接的方法

[英]Trying to stub chained methods for a model in Rspec 3

I am really new to Rspec and tried to find my answer, but it keeps on pointing me to use stub_chain, but it seems like it is deprecated on Rspec3. 我对Rspec真的很陌生,并试图找到我的答案,但是它不断指出我使用stub_chain,但似乎Rspec3上已弃用它。 I have the following I am trying to stub: 我尝试进行以下操作:

active_automation = Client.automation_active_status.new_client

Where Client is my model and automation_active_status is the following in my Client model 在我的客户端模型中,其中Client是我的模型,而automation_active_status是以下模型

scope :automation_active_status, -> { where(automation_status: true) }

new_client is an attribute I want to call to further filter my result new_client是我想调用的属性,以进一步过滤结果

I tried to implement stub_chain but that did not work. 我试图实现stub_chain,但这没有用。 My goal is to get the following to work: 我的目标是使以下各项起作用:

Client.any_instance( black_box_I_can_not_figure_out ).returns[something]

Thank you. 谢谢。

I believe you might be looking for allow and receive_message_chain . 我相信您可能正在寻找allowreceive_message_chain

allow(Client).to receive_message_chain(:automation_active_status, :new_client) { [thing_to_return] }

This will stub the methods allowed it, and return whatever comes out of the block you pass it. 这将对允许的方法进行存根,并返回您通过它的块中出现的所有内容。 Hope it helps. 希望能帮助到你。

Client.stub_chain(:automation_active_status, :new_client).and_return([thing-to-return]) should get you where you're trying to get. Client.stub_chain(:automation_active_status, :new_client).and_return([thing-to-return])应该可以帮助您到达想要的位置。

Additionally, any_instance should be used on instances of a class. 此外, any_instance应该用于类的实例 For instance, Client.first would be an instance, but Client is the class (and you can stub directly on it). 例如, Client.first将是一个实例,而Client是类(您可以直接在其上存根)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM