this post was submitted on 30 Jan 2026
580 points (97.4% liked)

Programmer Humor

29000 readers
561 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Tamo240@programming.dev 19 points 1 day ago (1 children)

Exactly, have fun trying to get test coverage without dependency injection

[–] jjjalljs@ttrpg.network 7 points 1 day ago (2 children)

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.

[–] wpb@lemmy.world 1 points 8 hours ago* (last edited 8 hours ago) (1 children)

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 1 points 3 hours 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.

[–] Tamo240@programming.dev 4 points 1 day ago (1 children)

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 2 points 23 hours ago (1 children)

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 1 points 22 hours ago (2 children)

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.

[–] SlurpingPus@lemmy.world 1 points 6 hours ago* (last edited 6 hours 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.

[–] jjjalljs@ttrpg.network 1 points 16 hours ago (1 children)

Javascript has mocking with jest: https://jestjs.io/docs/mock-functions

There's an example there of mocking our axios (a common library for network requests, a la python requests)

It's been a long time since I've used java, but mockito exists: https://site.mockito.org/javadoc/current/org/mockito/Mockito.html#2

(Usage note for anyone unfamiliar, but despite the name java and JavaScript are radically different languages.)

[–] Tamo240@programming.dev 1 points 6 hours ago* (last edited 6 hours ago)

I feel like there are two concepts be at confused here. 'Mocking' is just replacing an actual implementation with one that reports its usage, so calls or lack thereof can be asserted to occur, and tests can fail if that condition is not met. They usually allow setting side effects and return values on a per call basis also, to inject different behaviours for covering different code paths easily.

The question is then how do I get a class like DatabaseWrapper to call into an underlying mockDB instead of the normal realDB? The answer, in static languages is dependency injection: the db object must be constructed externally to the wrapper, and passed in in such a way that any object with the same interface is acceptable.

This allows the tests to pass in a mock with the same interface, and have the class being tested accept it. The class will then run as usual when its methods are called, but we can make assertions about how it uses its dependency. In some languages, such as python (and it seems JavaScript as well) this can be bypassed by monkey-patching the private member dynamically after the object has been constructed to be the mock instead of the real.

Personally, I don't think this leads to good design. Dependency injection also allows for a nice port and adapter pattern, were in the future we might replace our SQL database with a MongoDB one, and we have to rip up the application, instead of just implementing a new db class that meets the interface, and injecting that into the wrapper instead.