Skip to content Skip to sidebar Skip to footer

Django: Mock A Field On A Model

How do I assign a mock object to the user field on this model? Is the anyway to bypass the ''SomeModel.user' must be a 'User' instance' check? class SomeModel(models.Model):

Solution 1:

You can use unittest.patch to mock a property:

classTestThing(django.test.TestCase):
    @unittest.patch('my_app.models.SomeModel.user', new_callable=unittest.PropertyMock)deftest_thing(self, user_field):
        user_field.return_value = 5
        ...

This works on Django fields.

Solution 2:

You can't really bypass that check. The closest you can get is:

classSomeModel(models.Model):
    user_id = models.IntegerField(max_length=10)

Post a Comment for "Django: Mock A Field On A Model"