lib/rspec/rails/api/matchers.rb in rspec-rails-api-0.3.4 vs lib/rspec/rails/api/matchers.rb in rspec-rails-api-0.4.0
- old
+ new
@@ -2,30 +2,43 @@
require 'active_support/hash_with_indifferent_access'
require 'rspec/rails/api/utils'
+##
+# RSpec matcher to check something against an array of `expected`
+#
# FIXME: Split the matcher in something else; it's too messy.
RSpec::Matchers.define :have_many do |expected|
match do |actual|
@actual = actual
@actual = JSON.parse(actual.body) if actual.respond_to? :body
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?
+ # Primitive type
+ if expected[:type].is_a?(Symbol)
+ @actual.each do |item|
+ return false unless RSpec::Rails::Api::Utils.check_value_type(expected[:type], item)
+ end
+
+ return true
+ end
+
# Check every entry
- ok = true
@actual.each do |item|
- ok = false unless RSpec::Rails::Api::Utils.validate_object_structure item, expected
+ return false unless RSpec::Rails::Api::Utils.validate_object_structure item, expected
end
- ok
+ true
end
diffable
end
+##
+# RSpec matcher to check something against the `expected` definition
# FIXME: Split the matcher in something else; it's too messy.
RSpec::Matchers.define :have_one do |expected|
match do |actual|
@actual = actual
@actual = JSON.parse(actual.body) if actual.respond_to? :body