Skip to content

Commit

Permalink
Test example update (#236)
Browse files Browse the repository at this point in the history
* Test example update

because of @with_injector gone according to #146

Reported also on #220

* Cleanup
  • Loading branch information
pavlomorozov authored Oct 2, 2023
1 parent 395e7e8 commit 82929b6
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions docs/testing.rst
Original file line number Diff line number Diff line change
@@ -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
self.__injector = Injector(UsernameModule())

@inject
def test_username(self, username: str):
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.

0 comments on commit 82929b6

Please sign in to comment.