Rectangle 27 0

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

Thanks, but I do not like to use 3rd party modules for this purpose...

newPhoto.image = open(/path/to/foo.jpg)

python - Django testing model with ImageField - Stack Overflow

python django testing mocking
Rectangle 27 0

from django.core.files import File

newPhoto.image = File(open(image_path))

python - Django testing model with ImageField - Stack Overflow

python django testing mocking
Rectangle 27 0

For future users, I've solved the problem. You can mock an ImageField with an SimpleUploadedFile instance.

newPhoto.image = SimpleUploadedFile(name='test_image.jpg', content=open(image_path, 'rb').read(), content_type='image/jpeg')

python - Django testing model with ImageField - Stack Overflow

python django testing mocking