summaryrefslogtreecommitdiff
path: root/spec/support/matchers
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/matchers')
-rw-r--r--spec/support/matchers/event_store.rb12
-rw-r--r--spec/support/matchers/schema_matcher.rb34
2 files changed, 42 insertions, 4 deletions
diff --git a/spec/support/matchers/event_store.rb b/spec/support/matchers/event_store.rb
new file mode 100644
index 00000000000..96a71ae3c22
--- /dev/null
+++ b/spec/support/matchers/event_store.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+RSpec::Matchers.define :event_type do |event_class|
+ match do |actual|
+ actual.instance_of?(event_class) &&
+ actual.data == @expected_data
+ end
+
+ chain :containing do |expected_data|
+ @expected_data = expected_data
+ end
+end
diff --git a/spec/support/matchers/schema_matcher.rb b/spec/support/matchers/schema_matcher.rb
index 5e08e96f4e1..d2f32b60464 100644
--- a/spec/support/matchers/schema_matcher.rb
+++ b/spec/support/matchers/schema_matcher.rb
@@ -36,12 +36,38 @@ end
RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options|
match do |response|
- schema_path = Pathname.new(SchemaPath.expand(schema, dir))
- validator = SchemaPath.validator(schema_path)
+ @schema_path = Pathname.new(SchemaPath.expand(schema, dir))
+ validator = SchemaPath.validator(@schema_path)
- data = Gitlab::Json.parse(response.body)
+ @data = Gitlab::Json.parse(response.body)
- validator.valid?(data)
+ @schema_errors = validator.validate(@data)
+ @schema_errors.none?
+ end
+
+ failure_message do |actual|
+ message = []
+
+ message << <<~MESSAGE
+ expected JSON response to match schema #{@schema_path.inspect}.
+
+ JSON input: #{Gitlab::Json.pretty_generate(@data).indent(2)}
+
+ Schema errors:
+ MESSAGE
+
+ @schema_errors.each do |error|
+ property_name, actual_value = error.values_at('data_pointer', 'data')
+ property_name = 'root' if property_name.empty?
+
+ message << <<~MESSAGE
+ Property: #{property_name}
+ Actual value: #{Gitlab::Json.pretty_generate(actual_value).indent(2)}
+ Error: #{JSONSchemer::Errors.pretty(error)}
+ MESSAGE
+ end
+
+ message.join("\n")
end
end