From 82929b6eca69eddf70a4d5c078947012a3ce31df Mon Sep 17 00:00:00 2001 From: Pavlo Morozov <6754248+pavlomorozov@users.noreply.github.com> Date: Mon, 2 Oct 2023 10:38:48 +0200 Subject: [PATCH] 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 --- docs/testing.rst | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/docs/testing.rst b/docs/testing.rst index 568cbc2..c5cca33 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -1,24 +1,23 @@ 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 - from injector import Module, with_injector, inject + from injector import Injector, Module + class UsernameModule(Module): def configure(self, binder): binder.bind(str, 'Maria') - class TestSomethingClass(unittest.TestCase): - @with_injector(UsernameModule()) - def setUp(self): - pass - @inject - def test_username(self, username: str): + class TestSomethingClass(unittest.TestCase): + + def setUp(self): + self.__injector = Injector(UsernameModule()) + + def test_username(self): + username = self.__injector.get(str) 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.