[rspec-users] Comparing files

Ben Mabey ben at benmabey.com
Sun Dec 12 00:51:43 EST 2010


On 12/11/10 2:03 AM, Matt Wynne wrote:
> On 10 Dec 2010, at 16:21, Ben Mabey wrote:
>
>> On 12/10/10 8:56 AM, Matt Wynne wrote:
>>> Hello folks,
>>>
>>> I'm writing some tests for file upload code. The files are binary, images mostly. I'm futzing around a bit, trying to figure out how to assert that the uploaded file is the same as some golden master. If I do this:
>>>
>>> File.read(uploaded_file_path).should == File.read(path_to_expected_file)
>>>
>>> Then when it fails, I get an ugly diff of the difference between the binary files. So I'm about to invent something of my own. Has anyone got a good pattern for doing this already?
>>>
>>> cheers,
>>> Matt
>> I would compare the file's MD5 (or other) hash.  It won't tell you what is different.. just that they aren't identical which is what I think you want.   So... something like:
>>
>> Digest::MD5.hexdigest(File.read(uploaded_file_path)).should == Digest::MD5.hexdigest(File.read(path_to_expected_file))
>>
>> -Ben
> Great minds, Ben :)
>
> I ended up with this:
>
> https://gist.github.com/736421
>
> cheers,
> Matt

Yeah, that is a keeper... I don't know why my last email took several 
hours to reach the mailing list.  Very odd...

-Ben


More information about the rspec-users mailing list