mmerickel / wired

A service locator implementation for Python.
https://wired.readthedocs.io
MIT License
17 stars 9 forks source link

How to monkeypatch the venusian scanner? #21

Closed pauleveritt closed 5 years ago

pauleveritt commented 5 years ago

I have a test that tries to use a monkeypatch to assert calls to the scanner.

However, I can't seem to intercept venusian.scanner correctly. No matter what, it seems something else is importing it before I get a chance at it. Any suggestions?

pauleveritt commented 5 years ago

@mmerickel I think the answer here is to make venusian an argument, rather than an import?

mmerickel commented 5 years ago

You need to monkeypatch it at the place it's imported. For example if a module does from venusian import Scanner then it's a local variable in that module, so you need to modify it there. For example monkeypatch.setattr(module, 'Scanner', DummyScanner()).

pauleveritt commented 5 years ago

It always seems to be too far way for me to catch the import, or at least it was last time. Something wound up importing wired.dataclass which then imported venusian. I'll try again, thanks.

mmerickel commented 5 years ago

Well the question is which specific reference to the scanner you're trying to patch. It's basically always a question of patching the reference in the module that's using the object/class.