【问题标题】:How can I get more detail on why a fixture is not loading?我怎样才能获得更多关于为什么夹具没有加载的详细信息?
【发布时间】:2010-05-25 21:50:44
【问题描述】:

我有一个似乎没有加载固定装置的 TestCase。

我在构建测试数据库时看到此错误:

No fixtures found.  
.............................................Problem installing fixture '/Users/Bryan/work/CNPROG/forum/fixtures/forum_fixtures.json': Traceback (most recent call last):  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 169, in handle  
    obj.save(using=using)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save  
    models.Model.save_base(self.object, using=using, raw=True)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/base.py", line 543, in save_base  
    created=(not record_exists), raw=raw)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 162, in send  
    response = receiver(signal=self, sender=sender, **named)  
  File "/Users/Bryan/work/CNPROG/forum/models.py", line 656, in record_ask_event  
    activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ASK_QUESTION)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/fields/related.py", line 302, in __get__  
    rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)  
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 341, in get  
    % self.model._meta.object_name)  
DoesNotExist: User matching query does not exist.  



class UserProfileTestCases(TestCase):  
    """These are tests to verify that refactoring of UserProfile is correct"""  
    fixtures = ['forum_fixtures.json'] # apparently this needs to be in fixtures/ directory.  
    def setUp(self):  
        self.client = Client()  
        if self.client.login(username='DickCheney', password='test'):   
            print "client.login DickCheney successful";    
        else:   
            print "client.login FAILED"  

由于某种原因,灯具没有加载。

夹具位于:
论坛/fixtures/forum_fixtures.json

如何输出夹具未加载的原因?

Traceback 表明这里正在发生一些事情:
文件“/Users/Bryan/work/CNPROG/forum/models.py”,第 656 行,在 record_ask_event 中

但我无法想象为什么这会影响灯具的加载。 当我查看代码时,record_ask_events 是通过 post_save 事件调用的。
我能够成功manage.py loaddata forum_fixtures 所以我相信我正确设置了它们。

【问题讨论】:

    标签: python django-testing


    【解决方案1】:

    使用以下命令运行更详细的测试: python manage.py test --verbosity=2

    我通过重新排列灯具的顺序来解决这个问题。

    如果您有相关对象,则需要确保具有最多关联的对象(例如 auth_user )在夹具中首先列出,因为在保存期间可能会调用相关对象。

    Django v1.2 只查找名为“initial_data”的固定装置。一次都没有搜索过'forum_fixtures.json'

    我将夹具名称更改为“initial_data.json”,现在可以使用了。

    【讨论】:

      【解决方案2】:

      我遇到了这个确切的问题。我的应用程序包含在我的设置文件的 INSTALLED_APPS 元组中。因此,根据 django 文档,它应该在 app 目录下的 fixtures 目录中搜索我放置在我的 TestCase 类的“fixtures”属性中的任何文件名。不工作。不想将我的夹具命名为“initial_data.json”,因为我只想在运行单元测试时使用它。

      【讨论】:

      • 根据 SO 指南,请避免发布“我也是”回复作为答案(我没有投反对票;只是提供帮助)。
      【解决方案3】:

      您需要更改您的 post_save 信号处理程序以尊重“原始”kwarg。如果您对信号处理程序执行以下操作,则您的信号处理程序在夹具加载期间不会导致错误:

      @receiver(post_save, sender=MyModel, dispatch_uid='MyModelUID')
      def handler(sender, *args, **kwargs):
          if kwargs.get('raw'):
              return
          else:
              ...stuff
      

      【讨论】:

        猜你喜欢
        • 2013-01-12
        • 1970-01-01
        • 1970-01-01
        • 2020-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        相关资源
        最近更新 更多