RSpec expects to get a method with an array, but the order doesn't matter

Suppose I have a method #sumthat takes an array and calculates the sum of all the elements. I bit him:

  before do
    expect(calculation_service).to receive(:sum?).with([1, 2, 3]) { 6 }
  end

Unfortunately, my test suit passes the array in random order. Due to this error occurs:

 Failure/Error: subject { do_crazy_stuff! }
   #<InstanceDouble() (CalculationService)> received :sum? with unexpected arguments
     expected: ([1, 2, 3])
          got: ([3, 2, 1])

Is it possible for a call to the stub method to ignore the order of the elements of the array? array_including(1, 2, 3)does not guarantee the size of the array, so this is probably not the best solution here.

+4
source share
1 answer

RSpec with, contain_exactly(1, 2, 3) , , :

expect(calculation_service).to receive(:sum?).with(contain_exactly(1, 2, 3)) { 6 }

" 1, 2, 3" ( ), RSpec 3 , . a_collection_containing_exactly:

expect(calculation_service).to receive(:sum?).with(
  a_collection_containing_exactly(1, 2, 3)
) { 6 }
+8

Source: https://habr.com/ru/post/1614730/


All Articles