Test example update (#236)

* Test example update

because of @with_injector gone according to https://github.com/python-injector/injector/issues/146

Reported also on https://github.com/python-injector/injector/issues/220

* Cleanup
This commit is contained in:
Pavlo Morozov 2023-10-02 10:38:48 +02:00 committed by GitHub
parent 395e7e8e53
commit 82929b6eca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 11 deletions

View File

@ -1,24 +1,23 @@
Testing with Injector Testing with Injector
===================== =====================
When you use unit test framework such as `unittest2` or `nose` you can also profit from `injector`. However, manually creating injectors and test classes can be quite annoying. There is, however, `with_injector` method decorator which has parameters just as `Injector` construtor and installes configured injector into class instance on the time of method call:: When you use unit test framework such as `unittest2` or `nose` you can also profit from `injector`. ::
import unittest import unittest
from injector import Module, with_injector, inject from injector import Injector, Module
class UsernameModule(Module): class UsernameModule(Module):
def configure(self, binder): def configure(self, binder):
binder.bind(str, 'Maria') binder.bind(str, 'Maria')
class TestSomethingClass(unittest.TestCase):
@with_injector(UsernameModule())
def setUp(self):
pass
@inject class TestSomethingClass(unittest.TestCase):
def test_username(self, username: str):
def setUp(self):
self.__injector = Injector(UsernameModule())
def test_username(self):
username = self.__injector.get(str)
self.assertEqual(username, 'Maria') self.assertEqual(username, 'Maria')
**Each** method call re-initializes :class:`~injector.Injector` - if you want to you can also put :func:`~injector.with_injector` decorator on class constructor.
After such call all :func:`~injector.inject`-decorated methods will work just as you'd expect them to work.