unittest.mock: How can I move datetime.datetime.now with different calls in the same method?

0

How can I proceed with a mock test in datetime.datetime.now with different calls in the same method?

In my test, the current date is returned.

Following is an example of the code to help you. Thank you in advance.

from datetime import datetime as dt

def metodo():
    return dt.today().strftime('%Y'), dt.today().strftime('%Y-%m-%d %X %z')


def test_metodo(self):
    expected = ('2018', "2018-12-14 12:34:56")
    mock_date = Mock(spec=mypackage.metodo)
    today.side_effect=list(expected)
    self.assertIsNotNone(metodo())
    self.assertEqual(expected, metodo())
    
asked by anonymous 14.12.2018 / 18:11

1 answer

0

I was successful with this:

@patch('mypackage.dt')
def test_metodo(self, mock_date):
    expected = ('2018', "2018-12-14 12:34:56")
    mock_date.today.return_value = mypackage.dt(2018, 12, 14, 12, 34, 56)
    mock_date.today.return_value.strftime.side_effect = list(expected)
    self.assertIsNotNone(metodo())
    self.assertEqual(expected, metodo())
    
15.12.2018 / 16:02