1

我使用单表继承,其中模型生产者派生自模型用户,而模型用户又派生自联系。联系人和用户有一些验证:

  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true, 
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }

对于联系人和

  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true

在用户模型中。

在我的 factory.rb 中,我让 FactoryGirl 构建了一系列“生产者”记录,如下所示:

factory :producer do
  sequence(:name)  { |n| "Producer #{n}" }
  sequence(:email) { |n| "producer_#{n}@example.com"}   
  password "foobar"
  password_confirmation "foobar"
end

然后在我做的规范中

    let(:producer) { FactoryGirl.create(:producer) }

这似乎没有通过验证:

  1) Client
     Failure/Error: let(:producer) { FactoryGirl.create(:producer) }
     ActiveRecord::RecordInvalid:
       Validation failed: Name can't be blank, Email can't be blank,  
       Email is invalid, Password digest can't blank, Password is too short  
       (minimum is 6 characters), Password confirmation can't be blank

FactoryGirl中没有分配字段吗?

4

0 回答 0