Writing unit tests is not trivial. In this course, you'll learn how to succeed using a powerful tool, Google Mock. It has become the standard mocking framework for C++ developers who want to write high quality code faster.
Writing unit tests looks simple in blogs, books, and tutorials. However, it can be quite difficult when attempted in the real world where real code has dependencies that make unit testing difficult. The solution is to isolate unit tests using mocks, stubs, and fakes. Using mocks is crucial for any developer who attempts to unit test his code. In this course, Advanced C++ Mocking Using Google Mock, you'll learn how to fake dependencies in C++ using Google Mock. First, you'll learn about how to make unit tests simpler and more predictable using mocks. Next, you'll explore how to run Google Mock with different unit testing frameworks, and cover mocking frameworks best practices. Finally, you'll learn useful unit testing patterns for existing legacy code. By the end of this course, you'll have a working knowledge of mocking frameworks and Google Mock, and you'll be able to efficiently unit test any code.
Dror is a Software developer, architect, and consultant who likes to help software developers write better code. He's a public speaker and blogger. Dror teaches, mentors, and writes code using clean code principles, TDD, and BDD.
Course Overview Hi everyone! My name is Dror Helper, and welcome to my Pluralsight course, Advanced C++ Mocking Using Google Mock. I'm a software developer, architect, and consultant who cares about his code and enjoys teaching other developers how to write better code. Mocking frameworks are crucial when writing unit tests. Without them, we cannot write good deterministic tests isolated from dependencies outside of our control. This course is a deep dive into mocking. We will learn how to unit test real C++ code using Google Mock. Some of the major topics that we will cover include why you need a mocking framework when writing unit tests, how to use fake objects to eliminate external dependencies, running Google Mock with different unit testing frameworks, mocking frameworks best practices, and useful unit testing patterns for existing legacy code. By the end of this course, you'll know how to test real-world C++ code using Google Mock. Before beginning this course, you should be familiar with C++. So in case you need a refresher, check out one of the excellent Pluralsight courses from the C++ track. In case you have never written unit tests before, I suggest you check my other course, C++ Unit Testing Fundamentals Using Catch. I hope you'll join me on this journey to learn about real-world unit testing, mocking, and Google Mock with the Advanced C++ Mocking Using Google Mock course at Pluralsight.
Getting Started Hello! I am Dror Helper, and this is the Advanced C++ Mocking Using Google Mock course. In this course, we're going to talk about unit testing in Google Mock. We'll discuss why mock objects are crucial to a unit testing success and how to use the Google Mock framework and Google Test in order to unit tests your C++ code. Unit tests are a proven software engineering practice that help the developer write high quality code faster. But you need more than just unit testing framework in order to write good unit tests. Only in rare cases can you succeed unit tests in your code without the help of a mocking framework. And in this course, I will show you why and how fake objects and mocking frameworks can help you write better code. We will learn how to use Google Mock with any C++ test framework. But there's more to it than just using a framework. I will discuss best practices and pitfalls to avoid when writing unit tests with fake objects and show you how Google Mock can help write clear and simple unit tests even when testing complex, seemingly impossible-to-test code.
Creating Fake Objects Hello! My name is Dror Helper, and this is the Advanced C++ Mocking Using Google Mock course. In this module, I'll explain about mocks, fakes, and stubs, and show how to create them using Google Mock. It's time to get our hands dirty. Finally, we get to write fake objects in order to isolate dependencies in our tests. In the first part, I'll explain the alternative to mocking, which is writing integration tests, and show why we still need and even prefer in some scenarios to use unit tests with fake dependencies. You might have noticed that I called the subject of this course Mock Objects, as well as mocks and sometimes even fakes. And so before we dive into Google Mock, I'd like to take the time and explain the difference between mocks, stubs, and fakes. I will show you that you have always known how to write mocks. Then I will explain why we need a tool just like Google Mock and how to use it. The last part of this module will cover dependency injection. We'll discuss several ways to inject dependencies into production code and why it's important when using fake objects.
Setting Fake Object's Behavior Hello! I'm Dror Helper, and this is the Advanced C++ Mocking Using Google Mock course. In this module, I will focus on setting fake objects' behavior. We will start by discussing how a fake object behaves right out of the box, the default behavior of that fake object when it's first created by Google Mocking framework. Then we will see how we can change and adapt that default behavior for a specific instance or even a specific object type. After that, we will see how to define what a fake object will do during a test and how to harness this capability in order to create the exact scenario we need to test. At the end of this module, I'll cover a few best practices and common mistakes to avoid when using fake objects.
Verifying Behavior Hello! I'm Dror Helper, and this is the Advanced C++ Mocking Using Google Mock course. In this module, I will cover how to use mock objects to verify that a specific behavior occurred during a unit test run. I will start this module by showing you how to use Google Mock to check if methods were called during a test and why this is one of the most important features of any mocking framework. Then we'll dive into the difference between using state-based testing, that's when we use an assertion to check a return value from a method or a specific value on the object on the test, to interaction testing when we check that methods were called or in some cases not called during the test. As we cover how to use Google Mock for interaction testing, I will focus on each feature, explain the syntax and how to use it, and more importantly when to use it as using this powerful feature might lead to very fragile unit tests if overused or misused, and we don't want that. After this module, you should know enough about mocking in order to start writing unit tests using Google Mock. But you don't have to use Google Test if you don't want to. There are different unit testing frameworks you can use, and if you prefer a different unit testing framework, in the end of this module, I'll show you the steps needed in order to use Google Mock with a different C++ unit testing framework such as Catch.
Getting Your Legacy Code Under Control Hello! I'm Dror Helper, and this is the Advanced C++ Mocking Using Google Mock course. In this module, I will teach you patterns that will help you handle existing legacy code and show how even the most unmaintainable code can be tested using fake objects in Google Mock. We will begin this module by defining what is legacy code and explain about the challenges of working with legacy code followed by explaining about legacy code dilemma as it was defined in the book, Working Effectively With Legacy Code, by Michael Feathers. We will focus on the main challenges of testing legacy code and explain about what makes existing code difficult to test. In the second part of this module, I will show several useful patterns I have used to get legacy code under control. Most of those patterns are about how to inject fake objects into existing code without changing the code functionality or breaking existing functionality and in effect help us take legacy code and make it testable, readable, and maintainable.