- You have to change code under test so you can inject dependency. This can be done by static or dynamic polymorphism (I usually do a mix, but for simplicity lets do dynamic polymorphism):
class IDependency {
public:
virtual ~IDependency() {}
virtual void ModeMonitoring() = 0;
virtual void RadarStatusMonitoring() = 0;
virtual void CameraStatusMonitoring() = 0;
virtual void Supervision_Log(const ModeManagerType&) = 0;
};
class ProductionDependency : public IDependency {
public:
void ModeMonitoring() override {
::ModeMonitoring();
}
void RadarStatusMonitoring() override {
::RadarStatusMonitoring();
}
void CameraStatusMonitoring() override {
::CameraStatusMonitoring();
}
void Supervision_Log(const ModeManagerType& x) override {
::Supervision_Log(x);
}
};
ProductionDependency productionDependency;
void MainFunction(IDependency& dep = productionDependency)
{
// ....
dep.ModeMonitoring();
dep.RadarStatusMonitoring();
dep.CameraStatusMonitoring();
dep.Supervision_Log(ModeManager);
}
- Then in test provide a mock for dependency:
class MockDependency : public IDependency {
public:
MOCK_METHOD(void, ModeMonitoring, (), (override));
MOCK_METHOD(void, RadarStatusMonitoring, (), (override));
MOCK_METHOD(void, CameraStatusMonitoring, (), (override));
MOCK_METHOD(void, Supervision_Log, (const ModeManagerType&), (override));
};
- Finally in a test express required order:
TEST(MainFunction, callsInOrder)
{
MockDependency dep;
Expectation modeMonitoring = EXPECT_CALL(dep, ModeMonitoring());
EXPECT_CALL(dep, CameraStatusMonitoring()).After(modeMonitoring); // here order is forced
EXPECT_CALL(dep, RadarStatusMonitoring());
EXPECT_CALL(dep, Supervision_Log(_));
MainFunction(dep);
}
Here is doc about After.
Please do not specify to strict order. Make sure only required order is limited by test, for example order required by external API. Do not specify order of all calls based on current implementation, since this will limit your refactoring opportunities.
Here is live demo.