我正在为Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated.'
*** First throw call stack:
(0x180c0adb0 0x18026ff80 0x100a13e7c 0x1001bd54c 0x1001be77c 0x1860c288c 0x1860c2c3c 0x185eb78e8 0x185d775b4 0x185eb6d34 0x192375f40 0x185ef1c94 0x192375ccc 0x1001b97ac 0x1001bbe4c 0x1860bf030 0x1860bf198 0x1860ae298 0x1860c3c64 0x185e548c4 0x185d641e4 0x1836f698c 0x1836f15c8 0x1836f1488 0x1836f0ab8 0x1836f0818 0x1836e9ddc 0x180bc0728 0x180bbe4cc 0x180bbe8fc 0x180ae8c50 0x1823d0088 0x185dd2088 0x100121cc8 0x1806868b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
应用编写单元测试并使用
factory_boy
创建我的假测试数据。我在运行测试时遇到以下错误:
Django REST Framework
不确定它的含义或导致它的原因。这里定义了工厂:
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 254, in _find_tests
module = self._get_module_from_name(name)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
__import__(name)
File "/Users/thomasheatwole/osf-meetings/meetings/conferences/tests.py", line 27, in <module>
class SubmissionFactory(factory.DjangoModelFactory):
File "/Users/thomasheatwole/osf-meetings/meetings/conferences/tests.py", line 34, in SubmissionFactory
username = 'contributor'
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 67, in __call__
return cls.create(**kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 594, in create
return cls._generate(True, attrs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 519, in _generate
obj = cls._prepare(create, **attrs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 494, in _prepare
return cls._create(model_class, *args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/django.py", line 181, in _create
return manager.create(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 74, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 859, in _do_insert
using=using, raw=raw)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
cursor.execute(sql, params)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 323, in execute
return Database.Cursor.execute(self, query, params)
IntegrityError: UNIQUE constraint failed: auth_user.username
这里是创建实例的地方:
class UserFactory(factory.DjangoModelFactory):
class Meta:
model = User
这里是完整的文件:tests.py
让我知道是否有一个简单的解决方法。 谢谢!
答案 0 :(得分:0)
原来我打电话给工厂两次,所以两个用户都得到username = 'contributor'
。简单的解决方法是不在工厂内定义用户名,而是在调用工厂时定义它。
答案 1 :(得分:0)
正如您的回复中所述,问题在于您为unique=True
字段使用固定值。
一种解决方案是在每次调用时将username
传递给UserFactory
;但是,factory_boy
的目标是避免必须指定此类字段,除非测试需要特定值。
在您的示例中,您应该使用受以下内容启发的定义:
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.User
username = factory.Faker('username')
根据该定义,每次调用UserFactory
都会为User
提供不同的用户名。
并且,如果必须为特定测试设置username
,只需致电UserFactory(username='john')
。