如何删除“弃用:存根!已弃用.改用存根."信息? [英] How to remove "DEPRECATION: stub! is deprecated. Use stub instead." message?
问题描述
这个问题的表述有点奇怪,但我想要的是 Rspec 中 stub!
的替代方案,它不会产生弃用警告.
The question is a bit odd formulated but what I want is an alternative for stub!
in Rspec which doesn't produce a deprecation warning.
场景:
我使用 stub!
来存根我的辅助规范中的某些辅助方法.
I use stub!
to stub certain helper methods in my helper spec.
例如
stub!(:t_with_partner_lookup).and_return("test")
Rspec 建议使用不带感叹号的 stub
.
Rspec than suggests to use stub
without the exclamation mark.
所以我写(按照建议):
So I write (as suggested):
stub(:t_with_partner_lookup).and_return("test")
但是这会产生一个错误:
However this produces an error:
Stub :t_with_partner_lookup received unexpected message :and_return with ("test")
在我发现的另一个问题中,我必须使用 helper.
前缀.我做了,但它没有删除弃用警告,而是产生了一个错误.
In another question that I found, I had to use the helper.
prefix. I did, but it didn't remove the deprecation warning instead it produced an error.
helper.stub(:t_with_partner_lookup).and_return("test")
产生:
undefined method `t_with_partner_lookup' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_3:0x00000103256d50>
我也尝试过这种语法,但会产生与上述相同的错误:
I also tried this syntax but that produces the same error as noted above:
helper.stub(:t_with_partner_lookup){"test"}
存根辅助方法的正确语法是什么?
What is the correct syntax for stubbing a helper method?
我使用的宝石:
- 导轨 3.2.17
- 最新版本的 rspec-rails
Ruby 版本 2.1.0
Ruby version 2.1.0
推荐答案
已解决,使用允许语法:
Solved it, by using the allow syntax:
allow(self).to receive(:t_with_partner_lookup).and_return("test")
这篇关于如何删除“弃用:存根!已弃用.改用存根."信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!