C++ testing with mocks is EASY and awesome!

  Рет қаралды 19,681

Smok Code

Smok Code

Күн бұрын

Пікірлер: 42
@Smartskaft2
@Smartskaft2 4 жыл бұрын
The use of the adapter really blew my mind! So simple, yet effective. I've been skipping introducing interfaces when not having control of the source code of a class. Thanks!
@shurmurray
@shurmurray 3 жыл бұрын
And that is the very nature of OOP - spawn a handful of new classes full of boilerplate just to test one thing =)
@AhmedSam
@AhmedSam 3 жыл бұрын
PIMPL is an underrated idiom. I replaced so many switch cases with pointer to functions and classes. Pointer to class::member is also kinda of PIMPL
@coding6291
@coding6291 5 ай бұрын
Nice video, really helps me as a newbie to testing.
@Sebastian-qc3gw
@Sebastian-qc3gw 3 жыл бұрын
Great explanation! Helped me a lot, thanks Smoku!
@danieldesouzavicente7468
@danieldesouzavicente7468 4 жыл бұрын
Great video! So after introducing the interface the LegacyClass can return to the original non-virtual functions, right?
@hatulflezet
@hatulflezet 3 жыл бұрын
I know this is a "howto" for mocks. Never the less, it should be at least mentioned, that mocking should be used as last resort. And if the legacy code is large and complex, and it really is THAT hard to extract the methods you wanted to test in to class that only deals with them, you might need to mock. BUT: It should be said, that mocking is a hack! And that the mocking should be intermediate until you get to the point that the code is refactored, cleaned up, and then the mock should be removed and at best replaced with a fake. Why? Because mocking means you are making assumptions about the *implementation* of your *dependency*. And that is a smell of bad design. What you should first try to do, is to clean up the code you want to test and bring it to a state that a mock is not needed (that is, that you do not need to know what is going on in the dependency of your tested code), and that the dependency can be faked instead of being mocked. Fakes are far superior substitutes, exactly because they can do less. If you have a class that has a dependency, it should not care about HOW the dependency is implemented and thus, it would not need to mock. However because there is a dependency, a test can *fake* it, to deliver what ever functionality our class under test is expecting. Mocks are fragile, and bring maintenance cost since they are sensitive to the implementation of the object being mocked. If you have many mocks in your tests, this can become quiet tedious to maintain over time. Also, if the code under tests relies on implementation specifics of what it depends on, it means there is a tight coupling of between them, which again, is a bad design, and a rigid code. I would have loved if this disclaimer would at least have been mentioned.
@luizlfm
@luizlfm 2 жыл бұрын
I really don't like the distinction between mock and fake. I always say "mock" and I really always mean "fake", and I think most people do the same. I don't think many people are mocking in the sense that they try to replicate the dependency logic; that would be insane. The use of a Mock library is basically a shortcut for faking out dependencies; you create the "mock" and set it to return a fake value when called (if needed at all), you don't put any logic into it...
@TheCeglaaa
@TheCeglaaa 4 жыл бұрын
Very nice video! Well explained!
@KulaGGin
@KulaGGin 3 жыл бұрын
@Smok Good introduction. Looks like what I want. I want to do this with DirectX, which is Microsoft's code and not mine and I can't edit it at all. Adapter pattern it is. That's what I've been thinking. But my code is much more complex than this example and I can't quite get from this video to actually testing my code like this. So I'd like to get a list of connected DirectInput controllers. I can create an adapter to DirectX and methods I'd like to use. And I can create a mock. But how do I make this mock to work? I don't know how exactly DirectInput works. Can you recommend some course or a book which would actually provide realistic examples of how to do this kind of test-driven development when I depend on 3rd-party library? In my case it's DirectX 11 and 12.
@SmokCode
@SmokCode 3 жыл бұрын
Thanks. As to your question: I don't think there is something that would fit your scenario. These are very difficult things, and even this simplified video is a fairly advanced subject. The more "realistic" example you'll pick - the narrower the audience for it (hence it's hard to offer a good advice). Usually these things get hammered out by organisation and their best developers who carve out the rules that others follow. If you don't have anything like that in your org - start one.
@LastOfThem-d8b
@LastOfThem-d8b 3 жыл бұрын
This is great but what if the class is not virtual? Declare it as virtual with some macros for testing?
@luizlfm
@luizlfm 2 жыл бұрын
The implementation class is not virtual and never will be, you need to create the virtual (abstract) class to serve as the interface. This can be very confusing but the video does explain this quite well. Try to watch it another couple of times and copy his code, play around with it and you will get it.
@desvendandoornasaude4127
@desvendandoornasaude4127 11 ай бұрын
What IDE do you use?
@pizzabeerengineering2488
@pizzabeerengineering2488 4 жыл бұрын
Thanks for this. What are those UML cards made of?
@SmokCode
@SmokCode 4 жыл бұрын
Most of them are recycled from bad ideas and hours wasted on debugging.
@pizzabeerengineering2488
@pizzabeerengineering2488 4 жыл бұрын
@@SmokCode lol but what material? Looks more thick/rigid than paper
@SmokCode
@SmokCode 4 жыл бұрын
yup, photo paper glued with two layers of thick 160g satin paper
@pizzabeerengineering2488
@pizzabeerengineering2488 4 жыл бұрын
@@SmokCode Thanks : ) They look nice. Good for video props.
@ekrem8847
@ekrem8847 4 жыл бұрын
What team in amazon do you work for ? I believe that most team uses Java
@jaime7295
@jaime7295 Жыл бұрын
thank you, useful video
@pradeepparthiban9894
@pradeepparthiban9894 4 жыл бұрын
Hey Smok, Can you share the project somewhere ?
@alirezaaramoun615
@alirezaaramoun615 3 жыл бұрын
you should better mention that after adding the interface, we need to modify TEST too and pass the LegacyClass into TestUnit.
@anshitmishra
@anshitmishra 4 жыл бұрын
How do I view PPT files on the website using the Django framework?
@pablogilberto87
@pablogilberto87 4 жыл бұрын
what editor is that?
@SmokCode
@SmokCode 4 жыл бұрын
CLion from JetBrains
@logicboard7746
@logicboard7746 2 жыл бұрын
Genius!
@coljackdripperofburp
@coljackdripperofburp 2 жыл бұрын
mocking is a code smell. I don't want to spend half or more of my development time maintaining unit tests that have no relation to a user requirement, and add interfaces everywhere just to support tests which makes my code unreadable.
@megaman2016
@megaman2016 4 жыл бұрын
But what if legacy class gets updated?
@SmokCode
@SmokCode 4 жыл бұрын
One more reason for adapter - you don't have to update your code unless it's a breaking change, and even then all you have to update is your adapter. Profit!
@Darklighttd
@Darklighttd 4 жыл бұрын
Your engliah is the best. Great you.
@neilweidinger
@neilweidinger 4 жыл бұрын
Great video, but really reminds me why I hate oop
@SmokCode
@SmokCode 4 жыл бұрын
Yeah it's hard. But if you're in constrained environment - this is way better than plain C
@olokix
@olokix 4 жыл бұрын
we all love assembly, don't we? I'd like to code in bits directly... but you know... I'm such _a high class man_ ( ͡° ͜ʖ ͡°)
@KulaGGin
@KulaGGin 3 жыл бұрын
OOP? Assembly? Coding in bits? Edit byte code of physical memory on the machine in a text editor? It's all too high level. Just directly manipulate transistors.
@waziea1448
@waziea1448 3 жыл бұрын
wow
@ignacionr
@ignacionr 4 жыл бұрын
Most stupid idea. If the class is "legacy" it most likely means that you can't make its methods virtual.
@Smartskaft2
@Smartskaft2 4 жыл бұрын
Did you watch the entire video, or stopped when he mentioned that solution? The adapter would solve that issue.
@tk_const
@tk_const 4 жыл бұрын
Why are you so bitter?
@ignacionr
@ignacionr 4 жыл бұрын
@@Smartskaft2 exactly; maybe the video content could use some reorganising
@shubitoxX
@shubitoxX 3 жыл бұрын
@@ignacionr your comments could use some deletion
Regular Expressions: Classes and Sets
12:31
Smok Code
Рет қаралды 2,4 М.
Do you even test? (your code with CMake)
12:38
Code for yourself
Рет қаралды 25 М.
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 3,5 МЛН
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,6 МЛН
Creative Justice at the Checkout: Bananas and Eggs Showdown #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 8 МЛН
Mock anything in C++ without Dependency Injection
23:58
platis.solutions
Рет қаралды 2,9 М.
Thoughts About Unit Testing | Prime Reacts
11:21
ThePrimeTime
Рет қаралды 234 М.
Modern CMake for C++
11:38
Smok Code
Рет қаралды 48 М.
Unit Testing - How to Use Dummies, Fakes, Mocks, and Stubs
11:28
Introduction to Google Test and CMake
31:32
Rhymu's Videos
Рет қаралды 142 М.
Interview with a Senior C# Developer
10:56
Programmers are also human
Рет қаралды 650 М.
C++ cache locality and branch predictability
10:43
mCoding
Рет қаралды 85 М.
are "smart pointers" actually smart?
9:44
Low Level
Рет қаралды 78 М.
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 3,5 МЛН