【问题标题】:Testing django model clean method测试django模型清理方法
【发布时间】:2013-12-16 11:57:48
【问题描述】:

我有一个带有 clean 方法的模型,它检查两个字段是否为空,如果是则引发 ValidationError

我怎样才能对实例创建执行测试(通常我使用nose)而不会使测试因错误而失败。我想做一些断言来确保实例的创建失败。

这是干净的方法:

def clean(self):
    if not self.message and not self.image:
        raise ValidationError('You must provide either Message and/or '
                              'Image')

【问题讨论】:

    标签: django testing nose


    【解决方案1】:

    使用assertRaises():

    instance = YourModel()
    self.assertRaises(ValidationError, instance.clean)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-21
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2018-05-10
      相关资源
      最近更新 更多