如何使用 FactoryBoy 实例化 Django AbstractBaseUser 的子类
How can I instantiate a Subclass of my Django AbstractBaseUser with FactoryBoy
我一直在尝试使用 Factory Boy 来简化单元测试的编写。但是,当我尝试实例化我的自定义用户 class
的实例时,该库不太合作
根据 enter link description here 的指南,自定义 class 继承自 AbstractBaseUser
和 PermissionsMixin
。
每次我都尝试像这样实例化这个class:
class LogoutTests(TestCase):
def setUp(self):
self.employee_password = "foo"
self.manager_password = "bar"
self.test_employee_user = EmployeeFactory.create(self.employee_password)
self.test_manager_user = ManagerFactory.create(self.manager_password)
但是,我不断收到此错误消息:
factory.errors.FactoryError: Cannot generate instances of abstract factory ManagerFactory; Ensure ManagerFactory.Meta.model is set and ManagerFactory.Meta.abstract is either not set or False.
我已经用其他模型对此进行了测试,只有我的自定义用户模型会生成此错误,因为它可能继承自 AbstractBaseUser
。有什么帮助吗?
作为参考,我的自定义用户 class:
class Employee(AbstractBaseUser, PermissionsMixin):
class Meta:
abstract = False
first_name = models.CharField(max_length=250, default="Thibault", blank=False)
last_name = models.CharField(max_length=250, default="Dupont", blank=False)
email = models.EmailField(max_length=250, unique=True, blank=False, null=True)
employee_number = models.OneToOneField(EmployeeNumber)
is_manager = models.BooleanField(default=False, blank=False)
is_employed = models.BooleanField(default=True, blank=False)
is_active = models.BooleanField(default=False)
objects = EmployeeManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.get_full_name()
def get_short_name(self):
return '{0}. {1}'.format(self.first_name[0], self.last_name)
def get_full_name(self):
return '{0} {1}'.format(self.first_name, self.last_name)
def email_user(self, subject, message, from_email=None, **kwargs):
send_mail(subject, message, from_email, [self.email], **kwargs
消息说您还没有告诉 ManagerFactory
应该创建什么模型:ManagerFactory.Meta.model
未设置。
工厂应该是这样的:
import factory
import factory.django
from myapp import models
class ManagerFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Manager
# Your declarations here
我一直在尝试使用 Factory Boy 来简化单元测试的编写。但是,当我尝试实例化我的自定义用户 class
的实例时,该库不太合作根据 enter link description here 的指南,自定义 class 继承自 AbstractBaseUser
和 PermissionsMixin
。
每次我都尝试像这样实例化这个class:
class LogoutTests(TestCase):
def setUp(self):
self.employee_password = "foo"
self.manager_password = "bar"
self.test_employee_user = EmployeeFactory.create(self.employee_password)
self.test_manager_user = ManagerFactory.create(self.manager_password)
但是,我不断收到此错误消息:
factory.errors.FactoryError: Cannot generate instances of abstract factory ManagerFactory; Ensure ManagerFactory.Meta.model is set and ManagerFactory.Meta.abstract is either not set or False.
我已经用其他模型对此进行了测试,只有我的自定义用户模型会生成此错误,因为它可能继承自 AbstractBaseUser
。有什么帮助吗?
作为参考,我的自定义用户 class:
class Employee(AbstractBaseUser, PermissionsMixin):
class Meta:
abstract = False
first_name = models.CharField(max_length=250, default="Thibault", blank=False)
last_name = models.CharField(max_length=250, default="Dupont", blank=False)
email = models.EmailField(max_length=250, unique=True, blank=False, null=True)
employee_number = models.OneToOneField(EmployeeNumber)
is_manager = models.BooleanField(default=False, blank=False)
is_employed = models.BooleanField(default=True, blank=False)
is_active = models.BooleanField(default=False)
objects = EmployeeManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.get_full_name()
def get_short_name(self):
return '{0}. {1}'.format(self.first_name[0], self.last_name)
def get_full_name(self):
return '{0} {1}'.format(self.first_name, self.last_name)
def email_user(self, subject, message, from_email=None, **kwargs):
send_mail(subject, message, from_email, [self.email], **kwargs
消息说您还没有告诉 ManagerFactory
应该创建什么模型:ManagerFactory.Meta.model
未设置。
工厂应该是这样的:
import factory
import factory.django
from myapp import models
class ManagerFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Manager
# Your declarations here