2011-10-07 44 views
0

我已经运行我的程序,一切正常。我现在试图写一些测试,我一直得到这个错误:Rails 3中的编码错误

Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8 

我没有使用任何奇怪的字符,据我所知。我什至不知道从哪里开始这个错误。我试图将这两行添加到environment.rb文件中:

Encoding.default_internal = Encoding::UTF_8 
Encoding.default_external = Encoding::UTF_8 

并且没有帮助。下面是测试我试图运行:

require 'test_helper' 

class UserTest < ActiveSupport::TestCase 
    test "should require all fields" do 
    u = User.new 
    assert_false u.valid? 
    end 
end 

这里是我的users.yml里的文件:

one: 
    name: MyString 
    password: MyString 

two: 
    name: MyString 
    password: MyString 

<% (1..5).each do |i| %> 
    user_<%= i %>: 
    name: user_<%= i %> 
    password: user_<%= i %> 
<% end %> 

这里是我的堆栈跟踪:

Creating sqlite :memory: database 
-- create_table("comment_votes", {:force=>true}) 
    -> 0.0021s 
-- create_table("comments", {:force=>true}) 
    -> 0.0013s 
-- add_index("comments", ["post_id"], {:name=>"index_comments_on_post_id"}) 
    -> 0.0005s 
-- create_table("posts", {:force=>true}) 
    -> 0.0015s 
-- create_table("users", {:force=>true}) 
    -> 0.0013s 
-- create_table("votes", {:force=>true}) 
    -> 0.0010s 
-- initialize_schema_migrations_table() 
    -> 0.0009s 
-- assume_migrated_upto_version(20111005200722, ["db/migrate"]) 
    -> 0.0013s 

Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8 

1测试,0断言,0失败,1错误,0下降,0遗漏,0通知

测试套件完成:0.058747秒

以退出码1结束的程序

回答

0

您的密码是否被盐渍?并且盐是否会使用不符合UTF-8或US-ASCII的随机字节?这已经让我感觉到了。我必须在我的密码盐上输入force_encoding("BINARY")

+0

是的,我的密码是盐渍的。你把代码放在哪里? – user972276

+0

那么,我用DataMapper,而不是ActiveRecord,所以在我的情况下,它实际上是一个DM中的错误,我[提交了补丁](https://github.com/datamapper/dm-core/pull/141)。也许尝试定义'password_salt ='(或任何字段)和'password_salt'并在那里做? – d11wtq

+0

你可以发布你的密码salting和检查逻辑?那么我可能会帮助更多。 – d11wtq