I got tired of poring over the crappy output when comparing two large text blobs for equality fails; the more subtle the difference, the harder it is to spot it by eye. So I wrote this:

  def assert_equal_multiline_text(a, b, label = nil)
    return true if a == b
    label = label ? label + ', line' : 'Line'
    a_lines = a.split(/\n/)
    b_lines = b.split(/\n/)
    (0..([a_lines.length, b_lines.length].min - 1)).each do |i|
      assert_equal a_lines[i], b_lines[i], "#{label} #{i}:"

… where label is just a nice human-readable name for the things being compares. I call it like this:

assert_equal_multiline_text @expected.encoded,   Mailer.create_invoice(payment).encoded,   "Invoice mail"

My eyes are much happier.

blog comments powered by Disqus


02 October 2007


ruby/rails testing