-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Test example update because of @with_injector gone according to #146 Reported also on #220 * Cleanup
- Loading branch information
1 parent
395e7e8
commit 82929b6
Showing
1 changed file
with
8 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |