GTest GMock

 

Content

  •  Summary
  • Test Doubles
    • State Verification
      • Fake
      • Stub
      • Spy
    • Behavior Verification
      • Mock
  • Example --- ATM and Bank Server test case
    • Why we need to use Mock in this test case
      • We want to test the implementation of ATM.
      • ATM will connect to Bank Server.
      • We don't want to connect to the real Bank Server while testing. Therefore, we mock the Bank Server with GMock.
    • How to do it?
      • We need a base class for Bank Server.
      • We have a class for production Bank Server.
      • We have another class for test Bank Server.
      • We also need to define the behavior of the mock function in the test case.
  • What should the mock do?
    • User can define the mock behavior with the GTest macro
  • Test case structure
  • What does ON_CALL offer?
    • You can use it with Matchers
  • EXPECT_CALL vs ON_CALL
    • Compare
      • ON_CALL --- The call will not be verified
      • EXPECT_CALL --- The call will be verified
    • Which one should we use?
  • What does EXPECT_CALL offer?
    • Matchers / Composite Matchers
    • Wildcard Matcher: _
    • Cardinality --- User can call the function multiple time, user can define the return value one by one.
    • Uninteresting and Interesting Mock Functions
    • Uninteresting vs Unexpected
      • Meaning
        • Unexpected --- When the test case called a function that is not matched any mock rules.
        • Uninteresting --- That function is uninteresting it there's not even a single EXPECT_CALL
    • Enforcing the Order of Calls
  • GMock Actions
  • Sticky Rules
    • All rules remain active even after we have reached their invocation upper bounds.
  • GMock Advanced Topics
    • Checkpoints
    • Mocking Private or Protected Methods
    • Mocking Overloaded Methods
    • Mocking Template classes
    • Mocking Non-Virtual Classes
      • In this case, you cannot do dependency injection for unit testing.
      • You need to change your testing class into template based implementation. In this example, that will be the ATM class.
    • Mocking a Free Function
      • You will need to wrap it into a class with base class so that you can separate the test class and real class
    • Delegate to Fake
      • You need this when you have some functions you don't want to mock them.
    • Delegate to Real
  • Criticisms of Google Mock

All powerpoint slides

Learnt from here

Comments