I'm trying to mock a Django property named next_automatic_service_days
that returns a list of Date
objects. Here is how I do:
@freeze_time("2029-01-01")
def test_create_next_services_on_monday(self):
today = date.today()
with patch('restaurants.models.RestaurantParameters._next_automatic_service_days',
return_value=[today + td(days=i) for i in range(4)]):
# Call create_next_services method here
On models.py file:
class RestaurantParameters(models.Model):
class Meta:
abstract = True
@cached_property
def _next_automatic_service_days -> List[date]:
# ...
def create_next_services(self):
for day in self._next_automatic_service_days:
# We should enter this loop 4 times in the test
The problem is, we never enter the loop.
When I use ipdb in create_next_services
, I can see _next_automatic_service_days
is a magic mock, and the return value is a list of 4 items.
What is wrong with my code? Thanks.