require 'rspec' Person = Struct.new(:name, :age) RSpec.describe do subject { Person.new("Bob", 32) } it { is_expected.to have_attributes(name: "Bob", age: 33) } # ^ This failure prints the attributes diff as follows: # 1) is expected to have attributes {:age => 33, :name => "Bob"} # Failure/Error: it { is_expected.to have_attributes(name: "Bob", age: 33) } # # expected # to have attributes {:age => 33, :name => "Bob"} but had attributes {:age => 32, :name => "Bob"} # Diff: # @@ -1 +1 @@ # -:age => 33, # +:age => 32, # # # ./test_have_attributes_and.rb:9:in `block (2 levels) in ' it { is_expected.to be_a(Person).and have_attributes(name: "Bob", age: 33) } # ^ This failure prints the whole object rather than the attribute diff # 2) is expected to be a kind of Person and have attributes {:age => 33, :name => "Bob"} # Failure/Error: it { is_expected.to be_a(Person).and have_attributes(name: "Bob", age: 33) } # # expected # to have attributes {:age => 33, :name => "Bob"} but had attributes {:age => 32, :name => "Bob"} # Diff for (have attributes {:age => 33, :name => "Bob"}): # @@ -1,3 +1,2 @@ # -:age => 33, # -:name => "Bob", # +"#" # # # ./test_have_attributes_and.rb:12:in `block (2 levels) in ' end