0

In this Python project I'm working on I need to use a decorator in a method, but this decorator is only available in production environment and I need to test the class where it is used, so I first tried to mock the import as shown in How to mock an import but it didn't work for the decorator, I still get the error:

ImportError: No module named B

in the file I need to test.

I also tried to mock the decorator and pass it to the module as a dependency injection, but I get the error saying the decorator is not defined.

Any ideas?

  • 3
    As long as you don't share some code, nobody will be able to help you. – user2722968 May 24 '17 at 16:35
  • What does the decorator do? You wouldn't have a decorator that doesn't have any purpose. You could always write your own mock decorator to use in the tests. – Håken Lid May 24 '17 at 16:40
  • @HåkenLid the decorator only makes sense in production environment. It's about authenticated access permissions and for unit tests it's not relevant. The problem is that the code that uses this decorator imports it explicitly so I can't replace it for my own because then the code wouldn't work in production. – hsavietto May 25 '17 at 08:58
  • you can catch the ImportError and replace the failed import with your own dummy decorator. Something like this, maybe: `try: import my_decorator; except ImportError: my_decorator = lambda f: f` – Håken Lid May 25 '17 at 09:20
  • Possible duplicate of [What is a Pythonic way for Dependency Injection?](https://stackoverflow.com/questions/31678827/what-is-a-pythonic-way-for-dependency-injection) – Paul Sweatte Jul 05 '17 at 19:58

0 Answers0