RSpecでFailure時にEncoding::UndefinedConversionErrorが出る時の対処法

Ruby 1.9.3以降とか、特定のVerの組み合わせで出る症状と思われる。
もっと良い解決策があるんだろうと思うけど、以下のようにすれば
とりあえずは直る。

# gems/rspec-expectations-2.14.0/lib/rspec/expectations/differ.rb

# 変更前
def matching_encoding(string, source)
        string.encode(source.encoding)
end

# 変更後
def matching_encoding(string, source)
        string.force_encoding('UTF-8')
end


調べてみたところ、source.encodingがおかしくなることがあるようで、
encodeに失敗してしまうらしい。
会社の先輩がio.readが云々、みたいに言ってたけど、
もっと深く追いかければその辺に行き着くのかもしれない。

ちなみに…

def matching_encoding(string, source)
        string.encode(source.encoding, :undef => :replace)
end

こう書いても通るが、日本語などマルチバイト部分が読めなくなるので非推奨。