C++ testing with mocks is EASY and awesome!

preview_player
Показать описание
Using GTest and GMock in C++ can be easy with Dependency Injection and PIMPL idiom. Learn how you can properly make test doubles to create unit tests.

+
+
+

Support the channel and subscribe!
Рекомендации по теме
Комментарии
Автор

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!

Smartskaft
Автор

And that is the very nature of OOP - spawn a handful of new classes full of boilerplate just to test one thing =)

shurmurray
Автор

Nice video, really helps me as a newbie to testing.

coding
Автор

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

AhmedSam
Автор

Great video! So after introducing the interface the LegacyClass can return to the original non-virtual functions, right?

danieldesouzavicente
Автор

Great explanation! Helped me a lot, thanks Smoku!

Sebastian-qcgw
Автор

@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.

KulaGGin
Автор

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.

hatulflezet
Автор

This is great but what if the class is not virtual? Declare it as virtual with some macros for testing?

LastOfThem-db
Автор

What team in amazon do you work for ? I believe that most team uses Java

ekrem
Автор

you should better mention that after adding the interface, we need to modify TEST too and pass the LegacyClass into TestUnit.

alirezaaramoun
Автор

Thanks for this. What are those UML cards made of?

pizzabeerengineering
Автор

Hey Smok, Can you share the project somewhere ?

pradeepparthiban
Автор

How do I view PPT files on the website using the Django framework?

anshitmishra
Автор

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.

coljackdripperofburp
Автор

But what if legacy class gets updated?

megaman
Автор

Great video, but really reminds me why I hate oop

neilweidinger
Автор

Most stupid idea. If the class is "legacy" it most likely means that you can't make its methods virtual.

ignacionr