Biketire
Biketire

Reputation: 2069

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:

Ruby version 2.1.0

Upvotes: 6

Views: 2174

Answers (2)

Biketire
Biketire

Reputation: 2069

Solved it, by using the allow syntax:

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

Upvotes: 10

Rustam Gasanov
Rustam Gasanov

Reputation: 15781

I can suggest you to try

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

Upvotes: 1

Related Questions