Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate of any_number_of_times method in stubs

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)
like image 365
Adnan Ali Avatar asked Oct 07 '13 14:10

Adnan Ali


People also ask

What is the difference between stubs and mocks in Ruby testing?

Stub: A class or object that implements the methods of the class/object to be faked and returns always what you want. Mock: The same of stub, but it adds some logic that "verifies" when a method is called so you can be sure some implementation is calling that method.

What is the difference between mocks and stubs?

Mocks verify the behavior of the code you're testing, also known as the system under test. Mocks should be used when you want to test the order in which functions are called. Stubs verify the state of the system under test.


2 Answers

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.

like image 193
Peter Alfvin Avatar answered Nov 25 '22 09:11

Peter Alfvin


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.

like image 28
Adnan Ali Avatar answered Nov 25 '22 09:11

Adnan Ali