You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've have a custom matcher that I use when comparing hashes/ model objects.
The differ functionality is good, but when keys move around, especially when they aren't symbols and strings, it can sometimes be a little hard to get the information you are looking for. This becomes particularly useful when dealing with much large hashes or Rails model objects, as it only shows you the key value pairs that are incorrect, rather than all of them.
Here is an example of the current differ output and the output from my custom matcher. If there is interest in including it, I can share the rest of the code etc / potentially work on adding it as a feature.
example_spec.rb
require'spec_helper'describe'diff output for tags'dolet(:hash_1)do{:akey_1=>:value_1,:bkey_2=>:value_2,:bkey_3=>:value_3,:bkey_4=>:value_4,}endlet(:hash_2)do{:bkey_2=>:value_2,:akey_1=>:value_3,:akey_4=>:value_5,:bkey_3=>:value_3,}endit'outputs using the standard differ'doexpect(hash_1).toeqhash_2endit'outputs using the new differ'doexpect(hash_1).tomatch_hashhash_2endend
output.txt
1) diff output for tags outputs using the standard differ
Failure/Error: expect(hash_1).to eq hash_2
expected: {:bkey_2=>:value_2, :akey_1=>:value_3, :akey_4=>:value_5, :bkey_3=>:value_3}
got: {:akey_1=>:value_1, :bkey_2=>:value_2, :bkey_3=>:value_3, :bkey_4=>:value_4}
(compared using ==)
Diff:
@@ -1,5 +1,5 @@
-:akey_1 => :value_3,
-:akey_4 => :value_5,
+:akey_1 => :value_1,
:bkey_2 => :value_2,
:bkey_3 => :value_3,
+:bkey_4 => :value_4,
# ./spec/example_spec.rb:22:in `block (2 levels) in <top (required)>'
2) diff output for tags outputs using the new differ
Failure/Error: expect(hash_1).to match_hash hash_2
key: :akey_1 expected: :value_3 got: :value_1
key: :akey_4 expected: :value_5 got: <missing>
key: :bkey_4 expected: <missing> got: :value_4
# ./spec/example_spec.rb:26:in `block (2 levels) in <top (required)>'
Finished in 0.75406 seconds (files took 0.01402 seconds to load)
2 examples, 2 failures
Failed examples:
rspec ./spec/example_spec.rb:21 # diff output for tags outputs using the standard differ
rspec ./spec/example_spec.rb:25 # diff output for tags outputs using the new differ
Rerun all failed examples:
rspec ./spec/example_spec.rb:{21,25}
Interesting. How does your differ handle large values that don't fit neatly on one line (e.g. objects with long inspect output or multi-line strings)? What about when the difference is in some nested hash?
Currently it does nothing to handle really large output. That's not to say that it couldn't be updated to do so.
I also have an include_hash matcher as well, which is the more useful version that I use, that checks to see if the content of a smaller hash is included inside a larger one, allowing you to only check for specific key/value pairs instead against the whole hash, or slicing out a bunch of data beforehand.
I've have a custom matcher that I use when comparing hashes/ model objects.
The differ functionality is good, but when keys move around, especially when they aren't symbols and strings, it can sometimes be a little hard to get the information you are looking for. This becomes particularly useful when dealing with much large hashes or Rails model objects, as it only shows you the key value pairs that are incorrect, rather than all of them.
Here is an example of the current differ output and the output from my custom matcher. If there is interest in including it, I can share the rest of the code etc / potentially work on adding it as a feature.
An example with a much larger hash
The text was updated successfully, but these errors were encountered: