Rails- RSPEC不完整的多字节字符错误

时间:2014-01-17 03:29:24

标签: ruby-on-rails ruby ruby-on-rails-3 rspec fabrication-gem

我正在使用Rails 3.2.13和ruby 1.9.3。我一直收到以下错误:

Failure/Error: @user = Fabricate :user, role_ids: [role.id]
PG::Error:
   incomplete multibyte character
 # ./spec/controllers/api_controllers/projects_controller_spec.rb:6:in `block (2 levels) in <top (required)>'

关于错误的令人沮丧的是它间歇性地发生。有时我可以在没有发生的情况下运行测试10次,然后有时候每次都会发生。

出现错误的projects_controller_spec.rb文件第6行的代码如下所示:

require 'spec_helper'

describe Api::V1::ProjectsController do
  before do
    role = Fabricate :data_collector_role
    @user = Fabricate :user, role_ids: [role.id] # this is line 6
    @project = Fabricate.build(:project, proposal: Fabricate(:full_proposal))
    @project.save(validate: false)
    @permission = Fabricate :permission, user: @user, project: @project
    @project2 = Fabricate.build(:project, proposal: Fabricate(:full_proposal))
    @project2.save(validate: false)
    request.env["HTTP_ACCEPT"] = 'application/json'
    request.env['HTTP_AUTHORIZATION'] =   ActionController::HttpAuthentication::Token.encode_credentials(@user.api_key)
  end

上面第6行调用的Fabricator看起来像这样:

Fabricator :user do
    username { sequence(:username) { |n| "foo#{n}" } }
    password 'foobar'
    password_confirmation 'foobar'
    email_address "user@example.com"
    home_phone "123456789"
    work_phone "456789189"
    sales_channel
end

在其他不适用于API控制器的规范中调用相同的代码,它们可以100%的时间工作。

RSPEC一次又一次崩溃并发出以下错误:

     Failure/Error: get :show, { id: @project.id }
     ActiveRecord::StatementInvalid:
/Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/formatters/base_text_formatter.rb:182:in `split': invalid byte sequence in UTF-8 (ArgumentError)
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/formatters/base_text_formatter.rb:182:in `dump_failure_info'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/formatters/base_text_formatter.rb:175:in `dump_failure'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/formatters/base_text_formatter.rb:19:in `block in dump_failures'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/formatters/base_text_formatter.rb:17:in `each'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/formatters/base_text_formatter.rb:17:in `each_with_index'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/formatters/base_text_formatter.rb:17:in `dump_failures'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:98:in `block in notify'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:97:in `each'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:97:in `notify'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:81:in `finish'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/reporter.rb:36:in `report'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:25:in `run'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:66:in `rescue in run'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:62:in `run'
from /Users/michael/.rvm/gems/ruby-1.9.3-p448/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'

1 个答案:

答案 0 :(得分:0)

将其添加到文件spec / controllers / api_controllers / projects_controller_spec.rb的顶部

# !/usr/bin/env ruby
# encoding: utf-8