Module: RSpecApi::Matchers::Sort

Included in:
RSpecApi::Matchers
Defined in:
lib/rspec-api/matchers/sort/matcher.rb,
lib/rspec-api/matchers/sort/be_sorted.rb

Defined Under Namespace

Classes: Matcher

Instance Method Summary collapse

Instance Method Details

#be_sorted(options = {}) ⇒ Object

Passes if the object has a non-empty sorted JSON collection in the body.

Sorting is ascending unless verse.to_s is 'desc' or 'descending'

Examples:

Passes if the body is sorted by descending IDs

require 'rspec-api-matchers'

body = '[{"id": 3}, {"id": 2}, {"id": 1}]'
obj = OpenStruct.new body: body

describe 'be_sorted' do
  include RSpecApi::Matchers::Sort
  it { expect(obj).to be_sorted(by: :id, verse: :desc) }
end

# => (rspec) 1 example, 0 failures

Parameters:

  • options (Hash) (defaults to: {})

    how the body is expected to be sorted.

Options Hash (options):

  • :by (Symbol or String)

    The JSON key to sort by

  • :verse (Symbol or String) — default: :asc

    The sorting direction

See Also:


28
29
30
# File 'lib/rspec-api/matchers/sort/be_sorted.rb', line 28

def be_sorted(options = {})
  RSpecApi::Matchers::Sort::Matcher.new options
end