rspec 中的模拟方法链 [英] Mocking chain of methods in rspec

查看:51
本文介绍了rspec 中的模拟方法链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一系列方法可以获取user 对象.我正在尝试模拟以下内容以在我的 Factory Girl

There are a chain of methods which gets a user object. I am trying to mock the following to return a user in my Factory Girl

@current_user = AuthorizeApiRequest.call(request.headers).result

我可以模拟对象直到 call 方法,但我坚持模拟 result 方法

I can mock the object up until the call method but I'm stuck at mocking the result method

allow(AuthorizeApiRequest).to receive(:call).and_return(:user)

推荐答案

我发现我需要使用 receive_message_chain

I found I need to use receive_message_chain

所以这对我有用.

allow(AuthorizeApiRequest).to receive_message_chain(:call, :result).and_return(user)

这篇关于rspec 中的模拟方法链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆