2

我正在使用带有 ruby​​ 1.9.3 的 Rails 3.2.13。我不断收到以下错误:

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'
4

1 回答 1

0

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

# !/usr/bin/env ruby
# encoding: utf-8
于 2014-01-29T04:04:17.907 回答