I'm trying to instantiate a User instance through a Mock. That mock instance is being passed to another model Profile where I'm checking for any validation errors when a cleaning method is called.
However I'm getting: AttributeError: Mock object has no attribute '_state'
There is this previous post: How to mock users and requests in django. Nevertheless, I want to avoid any database calls.
What can be done differently so that a Mock will in this case?
#models.py
class Profile(models.Model):
hobby = "Hobbyist"
develop = "Developer"
coding_level = (
(hobby, hobby),
(develop, develop)
)
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
birth = models.DateField(verbose_name="Date Of Birth")
coding_level = models.CharField(
verbose_name="Experience",
max_length=20,
choices=coding_level, default=hobby, blank=False
)
bio = models.TextField(
verbose_name="User Bio",
validators=[MinValueValidator(10)]
)
github = models.URLField(
verbose_name="GitHub link",
validators=[check_submitted_link],
unique=True
)
avatar = models.ImageField(upload_to="images/%Y/%m/%d/")
#test_models.py
class TestProfile__001(SimpleTestCase):
def setUp(self):
self.test_user = Mock(
spec=User,
username="test_username",
email="test@email.com"
)
self.profile_data = {
'user': self.test_user,
'birth': '2019-10-07',
'coding_level': 'hobbyist',
'bio': "",
'github': "http://www.test.com",
'avatar': "image.txt"
}
def test_create_profile_fail(self):
with self.assertRaises(ValidationError):
test_profile = Profile(**self.profile_data)
test_profile.clean_fields()