How To Expand Ruby Rspec Difference Output

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.