How to remove “DEPRECATION: stub! is deprecated. Use stub instead.” message?

核能气质少年 提交于 2020-01-01 09:53:46

问题


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.

The scenario:

I use stub! to stub certain helper methods in my helper spec.

For instance

stub!(:t_with_partner_lookup).and_return("test")

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")

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")

Produces:

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?

Gems I use:

  • rails 3.2.17
  • most recent version of rspec-rails

Ruby version 2.1.0


回答1:


Solved it, by using the allow syntax:

allow(self).to receive(:t_with_partner_lookup).and_return("test")



回答2:


I can suggest you to try

helper = Object.new.extend SomeHelper
helper.stub(:t_with_partner_lookup).and_return('test')


来源:https://stackoverflow.com/questions/22373018/how-to-remove-deprecation-stub-is-deprecated-use-stub-instead-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!