我正在使用 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'
将其添加到文件规范/控制器/api_controllers/projects_controller_spec.rb 的顶部
# !/usr/bin/env ruby
# encoding: utf-8