FactoryGirl 无法分配给经过验证的枚举参数
FactoryGirl cannot assign to a enum validated parameter
我正在使用 RSpec、FactoryGirl 和 ActiveRecord。以下测试未通过:
require 'spec_helper'
describe User, type: :model do
it "has a valid factory" do
expect(FactoryGirl.create(:user)).to be_valid
end
end
我有以下错误:
1) User has a valid factory
Failure/Error: expect(FactoryGirl.create(:user)).to be_valid
ActiveRecord::RecordInvalid:
Validation failed: Role can't be blank
我还有以下型号:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
enum role: [:admin, :student, :school]
validates :role, presence: true
end
我有以下工厂:
require 'faker'
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "person#{n}@example.com" }
password Faker::Internet.password(8, 16)
role :student
end
end
但是,如果我删除模型中的枚举,它就会通过:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :role, presence: true
end
我想保留枚举,因为我只想允许一组字符串代表角色。
如果能提供有助于我正确合并 FactoryGirl 与枚举的信息,我将不胜感激。
在用户的出厂定义中,您使用 role :student
设置了角色。枚举存储为整数,而不是字符串 - 为了使您的定义有效,您需要像 role User.roles["student"]
.
这样的东西
有关详细信息,请参阅 http://api.rubyonrails.org/classes/ActiveRecord/Enum.html。
使用 @eugen 的回答,我现在明白我应该将列创建为整数,并且我已将其声明为字符串:
class AddRoleToUser < ActiveRecord::Migration
def change
add_column :users, :role, :string, null: false
end
end
我运行以下迁移来修复我的错误并且有效:
class ChangeRoleRepresentationInTable < ActiveRecord::Migration
def change
change_column :users, :role, 'integer USING CAST(role AS integer)', null: false
end
end
我正在使用 RSpec、FactoryGirl 和 ActiveRecord。以下测试未通过:
require 'spec_helper'
describe User, type: :model do
it "has a valid factory" do
expect(FactoryGirl.create(:user)).to be_valid
end
end
我有以下错误:
1) User has a valid factory
Failure/Error: expect(FactoryGirl.create(:user)).to be_valid
ActiveRecord::RecordInvalid:
Validation failed: Role can't be blank
我还有以下型号:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
enum role: [:admin, :student, :school]
validates :role, presence: true
end
我有以下工厂:
require 'faker'
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "person#{n}@example.com" }
password Faker::Internet.password(8, 16)
role :student
end
end
但是,如果我删除模型中的枚举,它就会通过:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :role, presence: true
end
我想保留枚举,因为我只想允许一组字符串代表角色。
如果能提供有助于我正确合并 FactoryGirl 与枚举的信息,我将不胜感激。
在用户的出厂定义中,您使用 role :student
设置了角色。枚举存储为整数,而不是字符串 - 为了使您的定义有效,您需要像 role User.roles["student"]
.
有关详细信息,请参阅 http://api.rubyonrails.org/classes/ActiveRecord/Enum.html。
使用 @eugen 的回答,我现在明白我应该将列创建为整数,并且我已将其声明为字符串:
class AddRoleToUser < ActiveRecord::Migration
def change
add_column :users, :role, :string, null: false
end
end
我运行以下迁移来修复我的错误并且有效:
class ChangeRoleRepresentationInTable < ActiveRecord::Migration
def change
change_column :users, :role, 'integer USING CAST(role AS integer)', null: false
end
end