设计和工厂女孩:Mysql2 ::错误:字段'密码'没有默认值:INSERT INTO`auth_user`

时间:2015-03-20 17:19:22

标签: ruby-on-rails ruby rspec devise factory-bot

我尝试使用FactoryGirl创建有效的auth_user,我的工厂看起来像这样:

FactoryGirl.define do

  factory :auth_user, class: AuthUser do
    username 'Test'
    first_name 'Wilson'
    last_name 'Mandela'
    email 'test@test.com'
    password 'password'
    password_confirmation 'password'
    is_staff true
    is_active true
    is_superuser true
    last_login DateTime.now
    date_joined DateTime.now
  end    
end

当我尝试在这样的Rspec中创建auth_user时:auth_user = create(:auth_user)

然后我收到以下错误:

Mysql2 ::错误:字段'密码'没有默认值:INSERT INTO auth_user

似乎Devise不接受工厂中的passwordpassword_confirmation字段。我还能做些什么来创建测试auth_user?我无法在Devise Wiki上找到任何帮助,但我可能忽略了它。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您收到的错误表明您的数据库表中有一个名为password的字段。这会给你带来麻烦,因为它会与Devise的同名虚拟属性冲突。

数据库中的字段应调用encrypted_password,这是您在运行rails generate devise auth_user时应创建的字段。如果情况并非如此,或者您添加了另一个名为password的字段,请修复迁移,然后运行rake db:migrate:redo VERSION=20150...etc-for-that-migration