Giter Club home page Giter Club logo

Comments (6)

alecthomas avatar alecthomas commented on August 31, 2024

The documentation is incorrect. It should be used on each method like so:

import unittest
from injector import Module, with_injector, inject

class UsernameModule(Module):
    def configure(self, binder):
        binder.bind(str, 'Maria')

class TestSomethingClass(unittest.TestCase):
    def setUp(self):
        pass

    @with_injector(UsernameModule())
    @inject(username=str)
    def test_username(self, username):
        self.assertEqual(username, 'Maria')
        print('foo')

if __name__ == '__main__':
    unittest.main()

Alternatively you could apply this to setUp(), and assign the injected attributes to fields.

I'll update the documentation.

from injector.

alecthomas avatar alecthomas commented on August 31, 2024

Though...that is quite annoying. It would be more useful if it did what the documentation suggests.

Fixed in 57d73f6.

from injector.

alecthomas avatar alecthomas commented on August 31, 2024

0.12.1 released with this change.

from injector.

tavianator avatar tavianator commented on August 31, 2024

Awesome, thank you!

from injector.

XFrankly avatar XFrankly commented on August 31, 2024

The documentation is incorrect. It should be used on each method like so:

import unittest
from injector import Module, with_injector, inject

class UsernameModule(Module):
    def configure(self, binder):
        binder.bind(str, 'Maria')

class TestSomethingClass(unittest.TestCase):
    def setUp(self):
        pass

    @with_injector(UsernameModule())
    @inject(username=str)
    def test_username(self, username):
        self.assertEqual(username, 'Maria')
        print('foo')

if __name__ == '__main__':
    unittest.main()

Alternatively you could apply this to setUp(), and assign the injected attributes to fields.

I'll update the documentation.

#===================================
Hi, I am trying to do this at injector 0.18.3, there are error like this:

import injector
injector.version
'0.18.3'
from injector import with_injector
Traceback (most recent call last):
File "", line 1, in
ImportError: cannot import name 'with_injector'

do you remove with_injector from injector.init ? I can not search for that also.

from injector.

outofthecave avatar outofthecave commented on August 31, 2024

@passjwhub The decorator @with_injector was removed in version 0.16.0, but the documentation was not updated. See #146 and #150

from injector.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.