1

I've patched datetime.datetime with MagicMock using the spec parameter.

patch(
  'datetime.datetime',
  MagicMock(spec=datetime, now=lambda: datetime.now() + timedelta(seconds=61)):
     action_involving_datetime.now()

Yet a isinstance(datetime.datetime.now(), datetime.datetime) fails with:

TypeError: isinstance() arg 2 must be a type or tuple of types

Because datetime.datetime still returns the MagicMock class. Any ideas?

martineau
  • 119,623
  • 25
  • 170
  • 301
Mahoni
  • 7,088
  • 17
  • 58
  • 115
  • Not closing the question as it's not an exact duplicate, but see https://stackoverflow.com/questions/11146725/isinstance-and-mocking – Selcuk Jul 25 '19 at 00:12
  • @Selcuk But the answer is saying I should use spec, and I do use spec. – Mahoni Jul 25 '19 at 00:14
  • Why did you patched the `datetime.datetime` instead of `your_module.datetime.datetime.now`? – Mauro Baraldi Jul 29 '19 at 19:55
  • @MauroBaraldi You're suggesting I should provision my own datetime type? I think this would still make the `isinstance` test fail – Mahoni Jul 29 '19 at 21:14
  • Take a look [here](https://gist.github.com/maurobaraldi/266435b877f6dd342b19280cdee8908e#file-mocks_for_humans-html-L319-L339) and see if it help. PS: the presentation is in portuguese... – Mauro Baraldi Jul 30 '19 at 14:07

0 Answers0