News for hackers. Stuff that matters.
Re:Don't Use Mocks by distcs on Tuesday June 20, 2023 @06:36AM (#36402068)
Can we not have articles like this with blanket statements like this please? As with everything in software, no one size fits all. Mocks have their place. There are situations when the only way we can test something out is to mock out a certain function call. Sometimes our dependencies are so complex and deep that we cannot just replace it with a fake. But with a little mock we can replace a function or a class within that dependency to decouple our tests from it.

Having a title with a blanket statement like "Don't Use Mocks" is either plain wrong or clickbait. In this case, it seems it is both wrong and clickbait. Such blanket statements and clickbaits specifically trigger people into getting a long-winded debate (ironically, my own comment here is a case in point) about something that is obvious and otherwise uncontroversial.

Re:Don't Use Mocks by smitty1e on Tuesday June 20, 2023 @07:26AM (#36402488)
I don't grasp the difference between a mock and a fake.

Both are substitutes for an expensive or unavailable component.

Maybe the fake is more dynamic than the mock?

The point seems moot.

Re:Don't Use Mocks by john-radio on Tuesday June 20, 2023 @07:40AM (#36402617)
As a Python main I'm wondering if the difference has to do with a strictly typed language like the author's two examples, Go and Java, requiring more boilerplate and interface classes and so on, that in turn require more customization of the mock (since he writes that it "implements the API of what it replaces" which seems like it would be a strange thing to say about a python mock).
Re:Don't Use Mocks by eximius on Tuesday June 20, 2023 @07:40AM (#36402623)
A fake is a 'local' or lower fidelity implementation of the real thing. A array/memory backed DB instead of postgres, etc.

A mock is a cache for a desired return value (which is hopefully what the real interface would correctly return).

Re:Don't Use Mocks by sethammons on Tuesday June 20, 2023 @07:37AM (#36402593)
Re:Don't Use Mocks by TeMPOraL on Tuesday June 20, 2023 @08:14AM (#36402929)
Mocks, fakes, stubs. Every time I read this article I end up more confused than I was before.
Re:Don't Use Mocks by Ma8ee on Tuesday June 20, 2023 @02:40PM (#36408580)
Re:Don't Use Mocks by sethammons on Tuesday June 20, 2023 @03:17PM (#36409085)
That is a solid way to introduce the different test doubles. Thanks for the link. I agree, stubs and spies are my daily bread. I also use fakes very regularly. I hardly ever use mocks, for the same reasons in the post.
Re:Don't Use Mocks by oslac on Tuesday June 20, 2023 @07:44AM (#36402657)
Mock has expectations about how the function is called. If you read a file from a disk, and you expect it only to be done once, a mock is "usable" in this scenario to count the number of invocations. Note that there aren't outside, observable, state changes or behavior involved in this. It is about the non-functional introspection.

Fake is just a simple implementation, like in-memory db to stand-in for a real one. In the optimal case, provided by library authors.

Re:Don't Use Mocks by slayerjain on Tuesday June 20, 2023 @08:19AM (#36402986)
I actually agree. I think the point the author is trying to make it that fake can be reused in more places and stay relatively same as the code updates.

But I personally think that instead of spending the time to write a fake, its better to just spend the time writing actual integration test with the real dependency (eg: just run the db in docker or something)

or if you don't want to spend that time, then just record the interaction (eg: db calls) and create "throwaway stubs". Use this stubs as long as they are relevant, and then generate new ones as your code grows. Save time "writing" any mocks, and you don't tend to couple too much with your mocks :P

Re:Don't Use Mocks by commandlinefan on Tuesday June 20, 2023 @10:25AM (#36404466)
> just run the db in docker

That creates unit tests that take ages to run - and fail surprisingly in hard to debug ways.

Re:Don't Use Mocks by zackees on Wednesday June 21, 2023 @11:39AM (#36419952)
If running a micro service use sqlalchemy and switch your db to sqlite for testing purposes.

There are scalability limits to this but for most cases it works extremely well and is fast.

Re:Don't Use Mocks by commandlinefan on Tuesday June 20, 2023 @10:24AM (#36404440)
What he calls a fake is what I call a mock - although I don't think he's necessarily building a strawman here, there are developers who use mocks the way he's telling you not to, and they are creating the problems that he outlines. I'd change it from "don't use mocks" to "don't be stupid with mocks".
Re:Don't Use Mocks by arp242 on Wednesday June 21, 2023 @04:31AM (#36415933)
How I understand it:

A mock is a set of fixed data; sometimes people even load these from YAML documents and the like. They're often re-used for different tests.

A fake is a "fake" object created when needed, with the parameters you need.

Mock:

  user = load_user_mock()
  article = load_article_mock()
  run_test(user, article)
Fake:

  user = newUser(Name: "foo", Email: "[email protected]")  # Or generate random data
  article = newArticle(User: user, Title: "bar")
  run_test(user, article)
The difference is somewhat subtle, but I often find mocks very inconvenient because you can't "just" change one without lots of stuff falling over. It's also much harder to do something "special" with them for that one test.
Re:Don't Use Mocks by xcdzvyn on Tuesday June 20, 2023 @08:03AM (#36402838)
Thank you. Seeing this on page 1 is just sad.
Re:Don't Use Mocks by mcherm on Tuesday June 20, 2023 @10:59AM (#36405020)
Sure, the title is clickbait. But the message seemed useful: if your test rigidly requires every call be made in exactly the way it is made now, then your test will be brittle. For a less brittle test, re-implement the functionality in a simpler form.

What this article did not do was to describe the trade-off: what you give up by creating a more complicated "fake" (to use the author's term).

Re:Don't Use Mocks by Maxion on Wednesday June 21, 2023 @02:20AM (#36415049)
A naive question here, should I not then in a unit test, test that an API library is called with specific arguments?

I am confused, wouldn't using simpler and "broader" tests miss test coverage on e.g. specific error handlers?

Re:Don't Use Mocks by boringuser2 on Tuesday June 20, 2023 @02:20PM (#36408264)
Actually, I feel that making strong statements of opinion regarding code style is probably the best way to go about doing things.