Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm not able to test my injectable class #146

Closed
mayermatheus opened this issue Jun 22, 2020 · 5 comments
Closed

I'm not able to test my injectable class #146

mayermatheus opened this issue Jun 22, 2020 · 5 comments

Comments

@mayermatheus
Copy link

Versions:

  • injector==0.18.3
  • Flask-Injector==0.12.3
  • python == 3.6.9

Problem:
I am looking to test a service which has the inject annotation in its constructor to be injectable. However, I am not able to test it. According to the documentation I need to do this:
import unittest
from injector import Module, with_injector, inject

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):
        self.assertEqual(username, 'Maria')

But copying and pasting this code without touching anything, gives the following problem:
image

Can anybody help me?

@jstasiak
Copy link
Collaborator

Ugh, sorry, I forgot to remove this part of the documentation. with_injector is gone. You need to create an instance of Injector on your own and use it, the simplest case

def test_username(self):
    injector = Injector(UsernameModule())
    assert injector.get('str') == 'Maria'

@jstasiak jstasiak reopened this Jun 22, 2020
@mayermatheus
Copy link
Author

@jstasiak It would look like this: from injector import Module, Injector, inject?

@jstasiak
Copy link
Collaborator

Yup

@mayermatheus
Copy link
Author

@jstasiak Could you update the documentation, please? I'm doing a task to insert the unit tests in the project kkkkk

@outofthecave
Copy link

Why is this issue closed? The docs in master are still incorrect: https://github.com/alecthomas/injector/blob/master/docs/testing.rst

pavlomorozov added a commit to pavlomorozov/injector that referenced this issue Oct 1, 2023
because of @with_injector gone according to python-injector#146

Reported also on python-injector#220
pavlomorozov added a commit to pavlomorozov/injector that referenced this issue Oct 1, 2023
because of @with_injector gone according to python-injector#146

Reported also on python-injector#220
davidparsson pushed a commit that referenced this issue Oct 2, 2023
* Test example update

because of @with_injector gone according to #146

Reported also on #220

* Cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants