r/node • • Nov 15 '17

Mocking is a Code Smell

https://medium.com/javascript-scene/mocking-is-a-code-smell-944a70c90a6a
14 Upvotes

9 comments sorted by

10

u/SomeRandomBuddy Nov 15 '17

This article is such a crock of shit. You can definitely use mocks without creating "tight coupling" so long as, as author says, the mocked objects also follow the SRP. This guy is basically waging war on imperative programming. Just because JavaScript contains elements of FP, it isn't a purely functional language.

In short, you can follow best practices and use mocks/inheritance simultaneously and his reasons for not doing it are strictly to avoid using mocks. Which isn't sound.

Can the unit be tested without mocking dependencies? If it can’t, it’s tightly coupled to the mocked dependencies.

LMAO

This guy must have just learned about functional react and decided he wanted some attention. I can't even with this.

5

u/RoboErectus Nov 15 '17

Some guy spent a lot of time and thought putting that article together. If you have a different point of view or think he's not seeing the full picture, I'm sure he'd love to learn.

It's awesome that someone put together this article to share their experience. It's even more awesome if they have more to learn.

It sounds like you have a lot you want to teach. I'd love to encourage you to do so!

5

u/SomeRandomBuddy Nov 15 '17

I respect the time and effort but also, by putting something in the public domain, you open it/yourself up to any criticisms that may follow. And for extreme hard-line pseudo-epiphany/borderline clickbait takes, that criticism tends to be more severe. The piece is long but it really just boils down to "you're an asshole if you write imperative code" which is absurdism at its finest. There are ways to use mocks and classes without introducing code smells, and with nothing more than basic tools such as modularity, single responsibility principle, and dependency injection.

The title and focus of the piece should have been "advantages of declarative/functional programming", but he went full sensationalist and said "why mocks are bad" which simply isn't true if you follow some extremely rudimentary best practices

2

u/fagnerbrack Nov 15 '17

"Enough talk, show me the code"

I'm not trying to be an ass, it's just that code can speak for a thousand words, and as commenters pointed out earlier, I really wanna know a good module that uses mocks and is better than composition.

2

u/spaghetti-in-pockets Nov 16 '17

"Enough talk, show me the code"

I agree, but turned back on the author. I think it's an interesting article, and I would like to see a more complete article using the theory. Instead of a few sum functions, how about one with http, file IO, etc?

1

u/[deleted] Nov 16 '17

I call people out for clickbait titles all the time, but I don't think this is one. He's arguing that mocks have uses, but those uses are fewer in number than the Agile consultants would have you believe.

If you adjust your thinking to consider mocks as a smell it means you will re-evaluate your code. There's a high probability it could be improved by considering why you need a mock at this point. Times I do use a mock; integration tests at service boundaries, dealing with legacy code, that's probably about it.

I'm what Fowler calls a classicist, so this "mocks are bad" mentality is not a new phenomenon. By bad we consider it a smell and a smell is bad until justified as necessary right?

I wouldn't read too much into the pseudo functional style of the code. It's written with Javascript in mind and pseudo functional is a popular style of coding. I'm more of a C# coder and we've been adopting aspects of functional programming just as long as the Javascript crowd. It's situationally beneficial, but it's not a magic bullet. Imperative code also benefits when you think of mocks as a smell.

6

u/afmrak Nov 15 '17

Any specific counterexamples to make the criticism constructive?

5

u/MoTTs_ Nov 16 '17 edited Nov 16 '17

Despite the article's title and long-winded digressions, it doesn't actually conclude that mocking is in any way bad. Ultimately, the article merely concludes that we often say "unit test" when we really mean "integration test".


Article explicitly states mocking is perfectly OK...

it’s perfectly OK to fake servers, network protocols, network messages, and so on

After the long-winded digressions, author finally gets to his one and only example where mocking is used, an express server with controllers. The "fix" wasn't to remove any of the mocks; it was to call it an integration test rather than a unit test. And voila, life is good again without having changed a thing.

Let’s look at a common example. People try to tell me that the express server definition file needs dependency injection because how else will you unit test all the stuff that goes into the express app? ... In order to “unit test” this file, we’d have to work up a dependency injection solution and then pass mocks for everything into it. ... The server definition file for an express app is by definition the app’s main integration point. Testing an express app file is by definition testing an integration between your program logic, express, and all the handlers for that express app. You absolutely should not skip integration tests even if you can achieve 100% unit test coverage.


This is the article of a salesman, someone who knows how to keep talking and get you saying "yes", even though his ultimate point is entirely fluff.

/u/fagnerbrack /u/SomeRandomBuddy /u/afmrak /u/RoboErectus

2

u/[deleted] Nov 15 '17

I read this a while ago, it's a really good article and I endorse its message.

It's Javascript specific, but the main thrust of the article and the solutions apply equally well to C# (probably other languages, JS and C# are my people).