简体   繁体   中英

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. 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

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

new_client is an attribute I want to call to further filter my result

I tried to implement stub_chain but that did not work. 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 .

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.

Additionally, any_instance should be used on instances of a class. For instance, Client.first would be an instance, but Client is the class (and you can stub directly on it).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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