[Django]-Mock function arguments in python

1👍

You need to @patch get_next_friday() function and substitute it’s return value with the one you need:

date_in_the_future = date.today() + timedelta(50)
next_friday_in_the_future = get_next_friday(base_date=date_in_the_future)

with patch('module_under_test.get_next_friday') as mocked_function:
    mocked_function.return_value = next_friday_in_the_future

    # call refresh_settlement_date
👤alecxe

4👍

An alternative approach would to be to patch the current date.

There is a relevant thread with multiple options:


My favorite option is to use a third-party module called freezegun.

You would need only one line to add, very clean and readable:

@freeze_time("2014-10-14")
def test_refresh_settlement_date_in_the_future(self):
    ...
👤alecxe

1👍

I just tried this out, it seems to work:

first I need to copy the function:

old_get_next_friday = get_next_friday

then patch it:

with patch.object(get_next_friday) as mocked_func:
    for i in range(8):
        mocked_func.return_value = old_get_next_friday(date.today() + timedelta(days=i))
        refresh_settlement_date()

Leave a comment