Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 587
Biketire Avatar asked Mar 13 '14 08:03

Biketire


2 Answers

Solved it, by using the allow syntax:

allow(self).to receive(:t_with_partner_lookup).and_return("test")
like image 148
Biketire Avatar answered Nov 04 '22 23:11

Biketire


I can suggest you to try

helper = Object.new.extend SomeHelper
helper.stub(:t_with_partner_lookup).and_return('test')
like image 1
Rustam A. Gasanov Avatar answered Nov 04 '22 22:11

Rustam A. Gasanov