4
4

More than 5 years have passed since last update.

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

Posted at

以前Blogにも書いたけど、こっちにも...。

割と最近のRuby + 割と最近のRSpec など、特定の組み合わせで発生するっぽい。
自分の環境は Ruby 1.9.3p327 + RSpec 2.14.4 だった。

臭いものにはフタを...な感じの修正だけど、とりあえず下記のようにすれば直る。
gems/rspec-expectations-{version}/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

なお、encodeの第2引数に「:undef => :replace」を書いてもエラーは出なくなるが、
日本語が読めなくなるのでNG。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4