Software APIs
Public Member Functions | Static Public Member Functions
global_mock::GlobalMock< Mock > Class Template Reference

Base class for mocks used in unit tests. More...

#include <sw/device/lib/base/global_mock.h>

Public Member Functions

 GlobalMock (const GlobalMock &)=delete
 
GlobalMockoperator= (const GlobalMock &)=delete
 
 GlobalMock (GlobalMock &&)=delete
 
GlobalMockoperator= (GlobalMock &&)=delete
 

Static Public Member Functions

static Mock & Instance ()
 

Detailed Description

template<typename Mock>
class global_mock::GlobalMock< Mock >

Base class for mocks used in unit tests.

If a class Mock derives from GlobalMock<Mock>, GlobalMock<Mock> ensures that there is at most one instance of Mock at a time (checked at runtime) and makes this instance globally accessible via the static Mock &Instance() method.

Mock classes should be globally accessible so that mock functions can call their methods during tests. They can also be strict or nice depending on tests' needs. Mock classes that satisfy both requirements can be defined as follows:

namespace global_mock {
namespace internal {
class MockFoo : public GlobalMock<MockFoo> {
  ...
};
}  // namespace internal
// Type alias for making `internal::MockFoo` a strict mock.
using MockFoo = testing::StrictMock<internal::MockFoo>;
// Type alias for making `internal::MockFoo` a nice mock if needed.
using NiceMockFoo = testing::NiceMock<internal::MockFoo>;
...
}  // namespace rom_test

This construction also ensures that we cannot have MockFoo and NiceMockFoo instantiated at the same time since they both derive from the same class, i.e. GlobalMock<internal::MockFoo>.

Definition at line 48 of file global_mock.h.