How do I compare two text files with RSpec?
Asked Answered
E

2

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?

Expunge answered 11/7, 2013 at 1:57 Comment(2)
What does your current method look like? You say you have a method that does 'x' so please do show your code.Sanborne
See sscce.org. You need to show code, sample data and any errors.Arlon
S
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.

Soap answered 11/7, 2013 at 2:3 Comment(1)
@Dom: I found it elsewhere and fixed the link.Soap
G
15

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

require 'fileutils'
expect(FileUtils.compare_file(file1, file2)).to be_truthy
Gypsy answered 19/4, 2015 at 23:9 Comment(0)
S
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.

Soap answered 11/7, 2013 at 2:3 Comment(1)
@Dom: I found it elsewhere and fixed the link.Soap

© 2022 - 2024 — McMap. All rights reserved.