Django testing model with ImageField

后端 未结 8 1645
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 16:52

I need to test the Photo model of my Django application. How can I mock the ImageField with a test image file?

tests.py

class PhotoT         


        
8条回答
  •  再見小時候
    2020-12-07 17:18

    Tell the mock library to create a mock object based on Django's File class

    import mock
    from django.core.files import File
    
    file_mock = mock.MagicMock(spec=File, name='FileMock')
    

    and then use in your tests

    newPhoto.image = file_mock
    

提交回复
热议问题