Alternate of any_number_of_times method in stubs
Asked Answered
Z

2

10

I have upgraded my project to rails 4 but now I am getting some deprecation warnings and one of them is DEPRECATION: any_number_of_times is deprecated.. Code for which I am gettings this warning is

sponsorship = RSpec::Mocks::Mock.new(:sponsorship)

SPONSORSHIP.should_receive(:[]).with('sponsorship').any_number_of_times.and_return(sponsorship)

and another scenario is

sponsorship.should_receive(:[]).with(key).any_number_of_times.and_return(value)

I have used stub for above code but it is not stubbing correctly. Can you find where I am doing it wrong. For stubbing I have used

SPONSORSHIP.stub(:[]).with('sponsorship').and_return(sponsorship)
Zetana answered 7/10, 2013 at 14:13 Comment(2)
Just a hunch, remove the any_number_of_times it makes sense that the should_receive would default to at least once.Selfreliant
removing any_number_of_times gives errors expected n* times got one timeZetana
C
16

The method any_number_of_times is deprecated (and is going away in RSpec 3) because it's not really testing anything. It will never fail, since it can be called 0 times as well. See extended argument in https://trello.com/c/p2OsobvA/78-update-website-menu-architecture-to-accommodate-pledging-as-well-as-weddings-memorials-etc.

If you expect it to be called at least once, you can use at_least(1).times.

Cates answered 7/10, 2013 at 15:11 Comment(1)
I've got a "Card not found" on this link.Esme
Z
3

Since any_number_of_times is not of any help other alternative methods like at_least(n) and at_most(n) helped removing those deprecation warnings.

Zetana answered 8/10, 2013 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.