• jjjalljs@ttrpg.network
    link
    fedilink
    arrow-up
    2
    ·
    10 days ago
    
    with patch("some_file.requests.get", side_effect=SomeException("oh no")):
      result = func_using_requests()
    

    Though not every language makes mocking as easy, and multiple responsibilities in a single function can quickly get messy.

    • Tamo240@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      9 days ago

      Oh I’m fully aware that python lets you cheat dependency injection with patch, its one of the many things python teaches developers to do wrong, which leads them to be unable to use any other language.

      • jjjalljs@ttrpg.network
        link
        fedilink
        arrow-up
        1
        ·
        9 days ago

        I vaguely remember Java also has mocking libraries, as does JavaScript. (Though JavaScript isn’t a language I’d hold up as the ideal.)

        • Tamo240@programming.dev
          link
          fedilink
          arrow-up
          0
          ·
          9 days ago

          While my experience is mostly C++, I assume these mocking libraries are similar in allowing you to create a class that can report it’s own usage, and allow for arbitrary returns values and side effects, which is incredibly useful, especially in conjunction with dependency injection.

          What patch lets you do is directly overwrite the functionality of private member functions on the fly, which if Java/JavaScript can do I’d love to know, I thought this was a uniquely Pythonic magic.

          • [object Object]@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            edit-2
            9 days ago

            JS is also just as dynamic in this regard. Both JS and Python create and modify objects at runtime and use object fields instead of static definitions of methods or properties. It’s probably inherited from Smalltalk.

    • wpb@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      9 days ago

      Quickest way to get a test suite so tightly coupled to structure rather than behavior that even just looking at the tests wrong makes them fail. But technically you do get test coverage I guess. Goodhart’s law in action.

      • jjjalljs@ttrpg.network
        link
        fedilink
        arrow-up
        1
        ·
        9 days ago

        It’s not really that different from like

        
        my_get_mock = Mock(side_effect=Some exception("oh no"))
        result = some_func(http_getter=my_get_mock)
        

        There’s many ways of writing bad code and tests, but mocks and patches aren’t always a bad tool. But sure, you can definitely fuck things up with them.