Giter Club home page Giter Club logo

Comments (3)

signalpillar avatar signalpillar commented on July 26, 2024 1

@md-weber what about this approach? I assume that there is some place where the injector is configured and, from my understand, it should be passed explicitly (or accessed as global) from the SubjectClass.

from unittest import mock 
def test_start():
    # Given
    mocked_controller = mock.Mock(spec=Controller)
    mocked_controller.get_project_by.return_value = Project("test_project", "test_owner", [], "0")

    mocked_repo = mock.Mock(spec=Repository)
    mocked_repo.start_optimization.return_value = ""

    def configure(binder):
        binder.bind(Controller, to=injector.InstanceProvider(mocked_controller))
        binder.bind(Repository, to=injector.InstanceProvider(mocked_repo))

    injector = Injector(configure)
    subject = SubjectClass(injector=injector)

    # When
    subject.start_optimization() 

    # Then
    mocked_repo.start_optimization.assert_called_once()
    

from injector.

jstasiak avatar jstasiak commented on July 26, 2024

Hi,

Replication with_injector's behaviour should be easy if you really need it but I'd like to understand the issue at hand first. Can you provide a code sample that demonstrates what you're doing? In general I don't think you should need or want to use injector in unit tests but we may be referring to different things using that term.

from injector.

md-weber avatar md-weber commented on July 26, 2024

I have a quite similar question to this topic, I work at the moment with the Injector and a Function that I want to test accesses the Injector.get and returns a class and calls a method. Now I would like to overwrite this method that it returns something that I want to set up in the first place.

    def test_start(self):
        subject = SubjectClass();
    
        Injector().get(Controller).get_project_by = Mock(
            return_value=Project("test_project", "test_owner", [], "0"))

       Injector().get(Repository).start_optimization = Mock(return_value="")

       subject.start_optimization() -> calls inside the Injector of Repository

        # The problems happens here
       Injector().get(Repository).start_optimization.assert_called_once()

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.