-1

I know:

require 'mocha/setup'

Kernel.stub(:rand, -1) do
  p Kernel.rand #=> -1
  p Kernel.rand #=> -1
  p Kernel.rand #=> -1
end

I need:

Kernel.stub(:rand, [-1, -2, -3]) do
  p Kernel.rand #=> -1
  p Kernel.rand #=> -2
  p Kernel.rand #=> -3
end

How can I program multiple future values for random in my test?

sKhan
  • 9,694
  • 16
  • 55
  • 53
Matrix
  • 3,458
  • 6
  • 40
  • 76

2 Answers2

0
Kernel.stubs(:rand).returns(-1, -2, -3)

http://gofreerange.com/mocha/docs/Mocha/Expectation.html#returns-instance_method

windock
  • 81
  • 4
  • don't works : NoMethodError: undefined method `stubs' for Kernel:Module, need a gem? – Matrix Mar 18 '16 at 03:13
  • That means that mocha is no loaded. Load it through tests or "require 'mocha/setup'" – windock Mar 18 '16 at 03:28
  • "NoMethodError: undefined method `__metaclass__' for Kernel:Module" when adding this require... – Matrix Mar 18 '16 at 03:30
  • the test run yes : "1) Error: CharacterTest#test_test: NoMethodError: undefined method `__metaclass__' for Kernel:Module" – Matrix Mar 18 '16 at 03:44
0

You could use sample method to return a random value from an array.

[-1,-2,-3].sample #=> -1
[-1,-2,-3].sample #=> -3
[-1,-2,-3].sample #=> -2
[-1,-2,-3].sample #=> -1
[-1,-2,-3].sample #=> -1

So you can do

Kernel.stubs(:rand).returns([-1,-2,-3].sample)
Rohit Jangid
  • 1,077
  • 1
  • 8
  • 19