Top Level Namespace

Defined Under Namespace

Modules: RSpec

Instance Method Summary collapse

Instance Method Details

#have_manyObject

RSpec matcher to check something against an array of expected



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rspec/rails/api/matchers.rb', line 11

RSpec::Matchers.define :have_many do |expected|
  match do |actual|
    actual = RSpec::Rails::Api::Utils.hash_from_response actual

    raise "Response is not an array: #{actual.class}" unless actual.is_a? Array
    raise 'Response has no item to compare with' unless actual.count.positive?

    @errors = RSpec::Rails::Api::Validator.validate_array actual, expected

    @errors.blank?
  end

  failure_message do |actual|
    object = RSpec::Rails::Api::Utils.hash_from_response(actual).to_json.chomp
    RSpec::Rails::Api::Validator.format_failure_message @errors, object
  end
end

#have_oneObject

RSpec matcher to check something against the expected definition



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rspec/rails/api/matchers.rb', line 31

RSpec::Matchers.define :have_one do |expected|
  match do |actual|
    actual = RSpec::Rails::Api::Utils.hash_from_response actual

    @errors = if expected.keys.count == 1 && expected.key?(:type)
                RSpec::Rails::Api::Validator.validate_type actual, expected[:type]
              else
                RSpec::Rails::Api::Validator.validate_object actual, expected
              end

    @errors.blank?
  end

  failure_message do |actual|
    object = RSpec::Rails::Api::Utils.hash_from_response(actual).to_json.chomp
    RSpec::Rails::Api::Validator.format_failure_message @errors, object
  end
end