Rails- RSPEC incomplete multibyte character error

363 Views Asked by At

I am using Rails 3.2.13 with ruby 1.9.3. I keep getting the following error:

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

The frustrating thing about the error is that it occurs intermittently. Sometimes I can run the tests 10 times without it happening and then other times it happens every time.

The code around line 6 of the projects_controller_spec.rb file where the error occurs looks like this:

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

The Fabricator that gets called on line 6 above looks like this:

Fabricator :user do
    username { sequence(:username) { |n| "foo#{n}" } }
    password 'foobar'
    password_confirmation 'foobar'
    email_address "[email protected]"
    home_phone "123456789"
    work_phone "456789189"
    sales_channel
end

The same code gets called in other specs that aren't for the API controllers and they work 100% of the time.

Every now and again RSPEC crashes completely and gives the following error:

     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

There are 1 best solutions below

8
On

Add this to the top of your file spec/controllers/api_controllers/projects_controller_spec.rb

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