frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

“Don’t mock what you don't own” in 5 minutes (2022)

https://hynek.me/articles/what-to-mock-in-5-mins/
27•JNRowe•2d ago

Comments

gnabgib•2d ago
(2022)
fellatio•4h ago
Polite way here is similar to inversion of control. It's a good idea but you need a real thing test somewhere but that can be running the program in its entirety, or using the facade end to end against the real interface for a spot check.
mpweiher•4h ago
Why I don't mock (2014): https://news.ycombinator.com/item?id=7809402

"Well, it's impolite, isn't it?"

AugustoCAS•4h ago
Something that I find amusing in the Java community is that a good number of senior developers, with anything from 5-20 years of experience, who do 'tdd' have never heard of the concept of test doubles and religiously think that a class must be tested in complete isolation mocking everything else.

The saddest one I saw was a team trying to do functional programming (with Spring). The tech lead was a bit flummoxed when I asked why mocks are not used in functional languages and continued to think that 'mocking functions' is the correct way to do TDD.

esailija•3h ago
Tests knowing about implementation details and testing the implementation details (which is the case 99.999% of the time if you use mocks) is more common than not. Even when the main value of automated testing is being able to change those very implementation details, that you now cannot do.

A whole bunch of work spent for no benefit or negative benefit is pretty common.

eximius•4h ago
And better yet, follow the tangential advice and use high fidelity fakes (I usually don't quite go all the way to verified fakes/contract tests, but they're a good idea if you have the time).

Real object where practical, fakes otherwise, mocks only as required for exceptional circumstances.

Works great in interconnected monorepos where you can provide high fidelity fakes of your services for other integrating teams to use in their tests. Often our service fakes are literally just the real service wrapped with an injected fake DB/store.

sparsely•3h ago
Fakes are better than mocks if only for the 100x superior debugging experience.
lmm•3h ago
Ok but the test is now not covering the parts that are actually going to break. And if you're only testing the parts above the nice business-friendly interface, you probably don't need mocks at all.
strken•3h ago
I'm confused. If I'm allowed to mock what I don't own, but only if it has a good API that's well-adapted to my use case, then "don't mock what you don't own" must have nothing to do with ownership.

It sounds like it's more about wrapping awkward APIs instead of calling them directly. Yes, this is good advice! But it only tangentially has anything to do with mocking.

molf•3h ago
I’m not sure this is good advice. I prefer to test as much of the stack as possible. The most common mistake I see these days is people testing too much in isolation, which leads to a false sense of safety.

If you care about being alerted when your dependencies break, writing only the kind of tests described in the article is risky. You’ve removed those dependencies from your test suite. If a minor library update changes `.json()` to `.parse(format="json")`, and you assumed they followed semver but they didn’t: you’ll find out after deployment.

Ah, but you use static typing? Great! That’ll catch some API changes. But if you discover an API changed without warning (because you thought nobody would ever do that) you’re on your own again. I suggest using a nice HTTP recording/replay library for your tests so you can adapt easily (without making live HTTP calls in your tests, which would be way too flaky, even if feasible).

I stopped worrying long ago about what is or isn’t “real” unit testing. I test as much of the software stack as I can. If a test covers too many abstraction layers at once, I split it into lower- and higher-level cases. These days, I prefer fewer “poorly” factored tests that cover many real layers of the code over countless razor-thin unit tests that only check whether a loop was implemented correctly. While risking that the whole system doesn’t work together. Because by the time you get to write your system/integration/whatever tests, you’re already exhausted from writing and refactoring all those near-pointless micro-tests.

strken•2h ago
My take on unit tests is that they happen "automatically" for the things that actually need unit testing. Consider a load shedder, a parser, or an implementation of an algorithm. Most devs will naturally write a unit test for anything with complicated logic just because it's easier to test complicated things in isolation.
hynek•2h ago
> I’m not sure this is good advice. I prefer to test as much of the stack as possible. The most common mistake I see these days is people testing too much in isolation, which leads to a false sense of safety.

You make it sounds as if the article would argue for test isolation which it emphatically doesn't. It in fact even links out to the Mock Hell talk.

Every mock makes the test suite less meaningful and the question the article is trying to answer is how to minimize the damage the mocks do to your software if you actually need them.

molf•2h ago
But ultimately it suggests this test; which only tests an empty loop?

  def test_empty_drc():
      drc = Mock(
          spec_set=DockerRegistryClient,
          get_repos=lambda: []
      )

      assert {} == get_repos_w_tags_drc(drc)
Maybe it's just a poor example to make the point. I personally think it's the wrong point to make. I would argue: don't mock anything _at all_ – unless you absolutely have to. And if you have to mock, by all means mock code you don't own, as far _down_ the stack as possible. And only mock your own code if it significantly reduces the amount of test code you have to write and maintain.

I would not write the test from the article in the way presented. I would capture the actual HTTP responses and replay those in my tests. It is a completely different approach.

hynek•2h ago
Yes, because it's showing how the simplest-possible mock already gets ugly if you don't follow the advice. Making the example more complicated would dilute the point it's making.

The question of "when to mock" is very interesting and dear to my heart, but it's not the question this article is trying to answer.

hamstergene•3h ago
It would be great if every complex or volatile third-party library came with its own mock and an abstract interface to which they both conform. Including pieces of language’s standard library itself.

It makes so much more sense this way:

- users don’t have to duplicate effort of writing more or less same mock/stub of the same library

- mock is forced to be up to date, because the library’s maintainers probably pay more attention to their release notes than you do

- no one knows the best choice of mocking approach/DSL than the library’s authors

- the adoption of inversion of control / dependency injection and comprehensive testing could be so much wider if writing a test didn’t require first solving an extra problem if how to mock something with complicated API and if it worth solving in the first place

ramchip•21m ago
This is very similar to Jose Valim's "Mocks and explicit contracts" from 2015, down to using a Twitter client as example.

https://dashbit.co/blog/mocks-and-explicit-contracts

Terpstra Keyboard

http://terpstrakeyboard.com/web-app/keys.htm
53•xeonmc•2h ago•17 comments

MiniMax-M1 open-weight, large-scale hybrid-attention reasoning model

https://github.com/MiniMax-AI/MiniMax-M1
181•danboarder•5h ago•37 comments

Scrappy - make little apps for you and your friends

https://pontus.granstrom.me/scrappy/
239•8organicbits•7h ago•78 comments

I counted all of the yurts in Mongolia using machine learning

https://monroeclinton.com/counting-all-yurts-in-mongolia/
82•furkansahin•4h ago•16 comments

Honda conducts successful launch and landing of experimental reusable rocket

https://global.honda/en/topics/2025/c_2025-06-17ceng.html
1111•LorenDB•21h ago•340 comments

The Grug Brained Developer (2022)

https://grugbrain.dev/
834•smartmic•16h ago•356 comments

Jiga (YC W21) Is Hiring Software Engs to Make Like of Mech Engs Easier

https://www.workatastartup.com/companies/jiga
1•grmmph•31m ago

Show HN: Lstr – A modern, interactive tree command written in Rust

https://github.com/bgreenwell/lstr
156•w108bmg•10h ago•43 comments

Introduction to the A* Algorithm

https://www.redblobgames.com/pathfinding/a-star/introduction.html
17•auraham•1d ago•2 comments

Building Effective AI Agents

https://www.anthropic.com/engineering/building-effective-agents
413•Anon84•18h ago•73 comments

A Straightforward Explanation of the Good Regulator Theorem

https://www.lesswrong.com/posts/JQefBJDHG6Wgffw6T/a-straightforward-explanation-of-the-good-regulator-theorem
25•surprisetalk•3d ago•2 comments

OpenSERDES – Open Hardware Serializer/Deserializer (SerDes) in Verilog

https://github.com/SparcLab/OpenSERDES
50•peter_d_sherman•8h ago•4 comments

3D-printed device splits white noise into an acoustic rainbow without power

https://phys.org/news/2025-06-3d-device-white-noise-acoustic.html
170•rbanffy•2d ago•39 comments

What Google Translate can tell us about vibecoding

https://ingrids.space/posts/what-google-translate-can-tell-us-about-vibecoding/
206•todsacerdoti•17h ago•113 comments

Preparation of a neutral nitrogen allotrope hexanitrogen C2h-N6

https://www.nature.com/articles/s41586-025-09032-9
15•bilsbie•2d ago•10 comments

Locally hosting an internet-connected server

https://mjg59.dreamwidth.org/72095.html
66•pabs3•7h ago•54 comments

Now might be the best time to learn software development

https://substack.com/home/post/p-165655726
249•nathanfig•21h ago•176 comments

Making 2.5 Flash and 2.5 Pro GA, and introducing Gemini 2.5 Flash-Lite

https://blog.google/products/gemini/gemini-2-5-model-family-expands/
329•meetpateltech•20h ago•190 comments

Resurrecting a dead torrent tracker and finding 3M peers

https://kianbradley.com/2025/06/15/resurrecting-a-dead-tracker.html
558•k-ian•18h ago•171 comments

Why JPEGs still rule the web (2024)

https://spectrum.ieee.org/jpeg-image-format-history
183•purpleko•21h ago•328 comments

LLMs pose an interesting problem for DSL designers

https://kirancodes.me/posts/log-lang-design-llms.html
177•gopiandcode•17h ago•109 comments

Proofs Without Words

https://artofproblemsolving.com/wiki/index.php/Proofs_without_words
65•squircle•4d ago•13 comments

Timescale Is Now TigerData

https://www.tigerdata.com/blog/timescale-becomes-tigerdata
126•pbowyer•21h ago•91 comments

Bzip2 crate switches from C to 100% Rust

https://trifectatech.org/blog/bzip2-crate-switches-from-c-to-rust/
288•Bogdanp•16h ago•132 comments

I Wrote a Compiler

https://blog.singleton.io/posts/2021-01-31-i-wrote-a-compiler/
80•ingve•3d ago•41 comments

Dinesh’s Mid-Summer Death Valley Walk (1998)

https://dineshdesai.info/dv/photos.html
68•wonger_•12h ago•24 comments

Time Series Forecasting with Graph Transformers

https://kumo.ai/research/time-series-forecasting/
102•turntable_pride•18h ago•32 comments

Strangers in the Middle of a City: The John and Jane Does of L.A. Medical Center

https://www.latimes.com/science/story/2025-06-15/l-a-seeks-help-for-a-patient-with-no-name
29•dangle1•2d ago•10 comments

Show HN: I made an online Unicode Cuneiform digital clock

https://oisinmoran.com/sumertime
93•OisinMoran•3d ago•28 comments

From SDR to 'Fake HDR': Mario Kart World on Switch 2

https://www.alexandermejia.com/from-sdr-to-fake-hdr-mario-kart-world-on-switch-2-undermines-modern-display-potential/
96•ibobev•17h ago•80 comments