Globalize + RSpec + factory_girl の使用で厄介な問題が発生しています。属性が変換されたモデルがあり、factory_girl を使用して工場を作成すると問題が発生します。コードはそれを完全に説明しています:
翻訳の移行:
def self.up
CandidateBranch.create_translation_table!({
name: {type: :string, null: false, limit: 150 }
}, {
migrate_data: true
})
end
モデル:
class CandidateBranch < ActiveRecord::Base
translates :name
####### Validations ---------------------------------------------------------
validates :name, presence: true, length: { in: 2..150 }
####### more code
end
工場:
FactoryGirl.define do
factory :candidate_branch do
sequence(:id) { |id| id }
sequence(:name) { “trying out" }
end
end
テスト:
require 'rails_helper'
RSpec.describe CandidateBranch, type: :model do
context "Validate" do
it "has a valid factory" do
record = FactoryGirl.attributes_for(:candidate_branch)
puts "parameters => #{record.inspect}"
record = CandidateBranch.create record
puts "parameters => #{record.inspect}"
expect(record).to be_valid
end
end
end
ログ:
▶ RAILS_ENV=test bundle exec rspec spec/models/candidate_branch_spec.rb
"parameters => {:id=>1, :name=>\"trying out\"}"
F
Failures:
1) CandidateBranch Validate has a valid factory
Failure/Error: record CandidateBranch.create record
ActiveRecord::StatementInvalid:
Mysql2::Error: Field 'name' doesn't have a default value: INSERT INTO `candidate_branches` (`id`, `created_at`, `updated_at`) VALUES (1, '2015-02-18 12:27:57.486623', '2015-02-18 12:27:57.486623’)
Mysql トランザクション:
(0.3ms) BEGIN
CandidateBranch::Translation Load (0.5ms) SELECT `candidate_branch_translations`.* FROM `candidate_branch_translations` WHERE `candidate_branch_translations`.`candidate_branch_id` = 1
(0.4ms) SAVEPOINT active_record_1
SQL (0.6ms) INSERT INTO `candidate_branches` (`id`, `created_at`, `updated_at`) VALUES (1, '2015-02-18 12:27:57.486623', '2015-02-18 12:27:57.486623')
Mysql2::Error: Field 'name' doesn't have a default value: INSERT INTO `candidate_branches` (`id`, `created_at`, `updated_at`) VALUES (1, '2015-02-18 12:27:57.486623', '2015-02-18 12:27:57.486623')
(0.2ms) ROLLBACK TO SAVEPOINT active_record_1
(0.3ms) ROLLBACK
レコードに見られるように、データベース クエリでレコードを作成するときにパラメーター属性 'name' が定義されていますが、変換テーブルは明らかに何も検出せず、フィールドを変換せずにレジストリを作成しようとしましたが、失敗しました。
ただし、モデル内の翻訳ステートメントをコメントすると…</p>
class CandidateBranch < ActiveRecord::Base
#translates :name
####### Validations ---------------------------------------------------------
validates :name, presence: true, length: { in: 2..150 }
####### more code
end
▶ RAILS_ENV=test bundle exec rspec spec/models/candidate_branch_spec.rb
"parameters => {:id=>1, :name=>\"trying out\"}"
"parameters => #<CandidateBranch id: 1, name: \"trying out\", created_at: \"2015-02-18 12:29:09\", updated_at: \"2015-02-18 12:29:09\”>"
MySQL トランザクション:
(0.3ms) BEGIN
(0.4ms) SAVEPOINT active_record_1
SQL (0.5ms) INSERT INTO `candidate_branches` (`id`, `name`, `created_at`, `updated_at`) VALUES (1, 'trying out', '2015-02-18 12:29:09.195756', '2015-02-18 12:29:09.195756')
(0.3ms) RELEASE SAVEPOINT active_record_1
(0.4ms) ROLLBACK
バグですか?私は何か間違ったことをしていますか?これは他の誰かに起こりましたか?