13

I have a method which compares if two text files have the same content.

How do I compare if two text files have the same content using RSpec?

Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73

2 Answers2

15

For others who stumble across this, check the FileUtils#cmp method:

require 'fileutils'
expect(FileUtils.compare_file(file1, file2)).to be_truthy
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
Jamon Holmgren
  • 23,738
  • 6
  • 59
  • 75
12

On a trivial level:

IO.read(file1).should == IO.read(file2)

If you want to do something nicer, you're likely going to need to write a new matcher, something like have_same_content_as defined to check for the above condition. "Up and Running with Custom RSpec Matchers" is a nice tutorial on writing custom matchers.

Amadan
  • 191,408
  • 23
  • 240
  • 301