From 7acb475414d23a0a903a11a73d6765701680cf43 Mon Sep 17 00:00:00 2001 From: Jakub Stasiak Date: Sat, 21 Sep 2013 00:06:49 +0100 Subject: [PATCH] Move testing information to Sphinx docs --- README.md | 28 ---------------------------- docs/index.rst | 1 + docs/testing.rst | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+), 28 deletions(-) create mode 100644 docs/testing.rst diff --git a/README.md b/README.md index 9f88fdd..81eaf30 100644 --- a/README.md +++ b/README.md @@ -180,34 +180,6 @@ You're probably thinking something like: "this is a large amount of work just to 1. Forces decoupling. In our example, this is illustrated by decoupling our configuration and database configuration. 2. After a type is configured, it can be injected anywhere with no additional effort. Simply `@inject` and it appears. We don't really illustrate that here, but you can imagine adding an arbitrary number of `RequestHandler` subclasses, all of which will automatically have a DB connection provided. - -Tests ------ - -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: - - -```pycon ->>> from injector import Module, with_injector ->>> class UsernameModule(Module): -... def configure(self, binder): -... binder.bind(str, 'Maria') -... ->>> class TestSomethingClass(object): -... @with_injector(UsernameModule()) -... def setup(self): -... pass -... -... @inject(username=str) -... def test_username(self, username): -... assert (username == 'Maria') - -``` - -*Each* method call re-initializes `Injector` - if you want to you can also put `with_injector` decorator on class constructor. - -After such call all `inject`-decorated methods will work just as you'd expect them to work. - Footnote -------- diff --git a/docs/index.rst b/docs/index.rst index 1d44893..6a2459e 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -26,6 +26,7 @@ Contents: changelog terminology + testing scopes logging api diff --git a/docs/testing.rst b/docs/testing.rst new file mode 100644 index 0000000..0a45da1 --- /dev/null +++ b/docs/testing.rst @@ -0,0 +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:: + + from injector import Module, with_injector + + class UsernameModule(Module): + def configure(self, binder): + binder.bind(str, 'Maria') + + class TestSomethingClass(object): + @with_injector(UsernameModule()) + def setup(self): + pass + + @inject(username=str) + def test_username(self, username): + assert (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.