Class: Synthra::SnapshotTesting::SnapshotMatcher
- Inherits:
-
Object
- Object
- Synthra::SnapshotTesting::SnapshotMatcher
- Defined in:
- lib/synthra/snapshot_testing.rb
Overview
RSpec matcher class
Instance Method Summary collapse
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(schema_name, snapshot_dir:) ⇒ SnapshotMatcher
constructor
A new instance of SnapshotMatcher.
- #matches?(actual) ⇒ Boolean
- #sort_hash(obj) ⇒ Object private
Constructor Details
#initialize(schema_name, snapshot_dir:) ⇒ SnapshotMatcher
Returns a new instance of SnapshotMatcher.
159 160 161 162 |
# File 'lib/synthra/snapshot_testing.rb', line 159 def initialize(schema_name, snapshot_dir:) @schema_name = schema_name @snapshot_dir = snapshot_dir end |
Instance Method Details
#failure_message ⇒ Object
177 178 179 |
# File 'lib/synthra/snapshot_testing.rb', line 177 def "expected generated data to match snapshot for #{@schema_name}" end |
#failure_message_when_negated ⇒ Object
181 182 183 |
# File 'lib/synthra/snapshot_testing.rb', line 181 def "expected generated data not to match snapshot for #{@schema_name}" end |
#matches?(actual) ⇒ Boolean
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/synthra/snapshot_testing.rb', line 164 def matches?(actual) @actual = actual path = File.join(@snapshot_dir, "#{@schema_name.downcase}.json") return false unless File.exist?(path) @expected = JSON.parse(File.read(path)) sorted_actual = sort_hash(@actual) sorted_expected = sort_hash(@expected) sorted_actual == sorted_expected end |
#sort_hash(obj) ⇒ Object (private)
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/synthra/snapshot_testing.rb', line 187 def sort_hash(obj) case obj when Hash obj.sort.to_h.transform_values { |v| sort_hash(v) } when Array obj.map { |v| sort_hash(v) } else obj end end |