我正在关注 Michael Hartl 的 Ruby on Rails Tutorial .我已经完成了教程,但现在当我尝试运行命令时
bundle exec rspec spec/
我收到一大堆 Encoding::UndefinedConversionErrors。这是一个例子:
101) Authentication AuthenticationPages signin with valid information followed by signout
Failure/Error: let(:user) { FactoryGirl.create(:user) }
ActiveRecord::StatementInvalid:
Encoding::UndefinedConversionError: "\xDC" from ASCII-8BIT to UTF-8: INSERT INTO "users" ("created_at", "email", "name", "password_digest", "remember_token", "updated_at") VALUES (?, ?, ?, ?, ?, ?)
# ./spec/requests/authentication_pages_spec.rb:33:in `block (5 levels) in <top (required)>'
# ./spec/requests/authentication_pages_spec.rb:34:in `block (5 levels) in <top (required)>'
我在 Stack Overflow 上搜索过类似的问题,但问题涉及文件 I/O,因此那里的解决方案并不适用于我的问题。我在运行 rspec 后阅读了一些 test.log 文件,我得到一个提示,它可能与我的数据上传到服务器的方式有关,那里发生了某种奇怪的转换错误。
这是我在 database.yml 中用来定义我的测试数据库的内容:
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
encoding: UTF8
我正在运行 Rails 4.0.0、Ruby 2.0.0 并使用 sqlite3 数据库。我的 Gemfile 中的 sqlite gem 是:gem 'sqlite3', '1.3.8'
。我的 rspec Gemfile 中的 gem 如下:gem 'rspec-rails', '2.13.1'
。
从本质上讲,我试图弄清楚为什么会出现这些错误以及如何让它们停止发生,但我对两者都不太确定。
编辑:这是我的 factory.rb 文件的内容:
#encoding: utf-8
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com" }
password "foobar"
password_confirmation"foobar"
factory :admin do
admin true
end
end
factory :micropost do
content "Lorem ipsum"
user
end
end
我还注意到我的 development.sqlite3 和 test.sqlite3 中有一些 ASCII 字符。使用 Notepad++,我尝试将文档重新编码为 UTF-8,但随后出现错误,要求我重置并重新迁移我的数据库。在我完成所有这些之后,开发和测试 SQLITE3 文件又恢复为包含 ASCII 字符。我不确定这是否与此有关。
最佳答案
您收到此错误是因为您试图将 ASCII-8BIT 字符放入 UTF-8 编码的数据库中。您是否更改了用户工厂文件中的某些内容?任何特殊字符,例如。 'Ü' 有吗?尝试放在你的工厂文件之上:
#encoding: utf-8
关于ruby-on-rails - Ruby on Rails RSpec 错误:Encoding::UndefinedConversionErrors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20573143/