这是一个 FactoryGirl 工厂:
FactoryGirl.define do
factory :idea do
title Faker::Lorem.sentence
note Faker::Lorem.sentences(2)
status "available"
end
end
这是idea
模型:
class Idea < ActiveRecord::Base
attr_accessible :note, :status, :title
validates :title, presence: true, uniqueness: true, length: {minimum: 20}
validates :status, presence: true, inclusion: {in: %w(pending available claimed overdue submitted aborted rejected)}
belongs_to :user
end
现在,当我输入我的 Rails 控制台t1 = FactoryGirl.create(:idea)
时,没问题,我明白了。但是当我输入它时t2 = FactoryGirl.create(:idea)
它崩溃了,说验证失败:ActiveRecord::RecordInvalid: Validation failed: Title has already been taken
事实上,我在 SQL 转储中看到 FactoryGirl 尝试使用相同的字符串两次:
1.9.3p327 :002 > t1 = FactoryGirl.create(:idea)
(0.0ms) begin transaction
Idea Exists (1.8ms) SELECT 1 AS one FROM "ideas" WHERE "ideas"."title" = 'Eligendi sint quod quia alias sed sit vitae repellendus.' LIMIT 1
SQL (7.4ms) INSERT INTO "ideas" ("created_at", "note", "status", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Thu, 27 Dec 2012 18:20:47 UTC +00:00], ["note", ["Aut placeat mollitia.", "Adipisci in est eos."]], ["status", "available"], ["title", "Eligendi sint quod quia alias sed sit vitae repellendus."], ["updated_at", Thu, 27 Dec 2012 18:20:47 UTC +00:00], ["user_id", nil]]
(6.3ms) commit transaction
=> #<Idea id: 1, title: "Eligendi sint quod quia alias sed sit vitae repelle...", note: ["Aut placeat mollitia.", "Adipisci in est eos."], status: "available", created_at: "2012-12-27 18:20:47", updated_at: "2012-12-27 18:20:47", user_id: nil>
1.9.3p327 :003 > t2 = FactoryGirl.create(:idea)
(0.1ms) begin transaction
Idea Exists (2.7ms) SELECT 1 AS one FROM "ideas" WHERE "ideas"."title" = 'Eligendi sint quod quia alias sed sit vitae repellendus.' LIMIT 1
(0.0ms) rollback transaction
ActiveRecord::RecordInvalid: Validation failed: Title has already been taken
Faker::Lorem.sentence
但是当我在控制台中反复运行时,我不断得到随机的、不同的句子。
那么,为什么 Faker 和/或 FactoryGirl 决定使用完全相同的字符串,即使它应该是随机的?