How To Expand Ruby Rspec Difference Output

Image for post
Image for post

TL;DR

This one goes into howto category, for all the testers in the void and future me. Explains how to set size of rspec diff output which is small by default.

In my rspec tests, I need to compare two rather large array of hashes. For that I am using hashdiff gem.

a = {a:3, b:2}
b = {}

diff = HashDiff.diff(a, b)
diff.should == [['-', 'a', 3], ['-', 'b', 2]]

You can see that hashdiff output is array of attribute differences. When you start to use hashdiff gem, you realize that describing the difference between two hashes is not and easy task.

Using rspec with default settings, I got following diff report:

require 'hashdiff'
describe Hash do
before do
@hash_a = {:hello => "world" * 100}
@hash_b = {:hello => 'world'}
end
it "should return truncated difference" do
expect(HashDiff.diff(@hash_a,@hash_b)).to eq []
end
end
[["~", "hello", "worldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworl...rldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworld", "world"]]

Note the three dots that hide most of the difference.

In order to expand rspec diff report, you need to put in rspec config section:

require 'hashdiff'
RSpec.configure do |rspec|
rspec.expect_with :rspec do |c|
c.max_formatted_output_length = 1000 # n is number of lines, or nil for no truncation.
end
end
describe Hash do
before do
@hash_a = {:hello => "world" * 100}
@hash_b = {:hello => 'world'}
end
it "should return truncated difference" do
expect(HashDiff.diff(@hash_a,@hash_b)).to eq []
end
end
[["~", "hello", "worldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworldworld", "world"]]

Originally published at tentamen software testing blog.

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store