-
Notifications
You must be signed in to change notification settings - Fork 35
Open
Description
Hi all and thanks for this beautiful gem,
Do you think it would be useful to enable somekind of mechanism allowing to prettify JSON output in case if matching process fails? If yes, what would be the best way to implement this?
Thanks for your suggestion!
My ugly solution is to include this snippet in test_helper.rb
:
module JsonExpressions
class Matcher
def json
@json
end
end
end
module MiniTest
module Assertions
def mu_pp2(obj, msg = nil)
ap msg, color: {string: :purpleish} if msg.present?
ap obj
ap msg.reverse, color: {string: :purpleish} if msg.present?
end
def assert_json_match(exp, act, msg = nil)
unless JsonExpressions::Matcher === exp
exp = JsonExpressions::Matcher.new(exp)
end
if String === act
assert act = JSON.parse(act), "Expected #{mu_pp(act)} to be valid JSON"
end
assert exp =~ act, ->{ "Expected #{mu_pp2(exp.json, 'Expected---------->')} to match #{mu_pp2(act, 'Actual---------->')}\n" + exp.last_error}
# Return the matcher
return exp
end
def refute_json_match(exp, act, msg = nil)
unless JsonExpressions::Matcher === exp
exp = JsonExpressions::Matcher.new(exp)
end
if String === act
assert act = JSON.parse(act), "Expected #{mu_pp(act)} to be valid JSON"
end
refute exp =~ act, ->{ "Expected #{mu_pp2(exp.json, 'Expected')} to match #{mu_pp2(act, 'Actual')}\n" + exp.last_error}
# Return the matcher
return exp
end
end
end
Jeehut, ethagnawl and dfl
Metadata
Metadata
Assignees
Labels
No labels