We have a class counting events. In order to do so, it registers a listener with an event manager in its constructor.
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class EventManager {
public:
class EventListener {
public:
virtual ~EventListener() = default;
virtual void onEvent() = 0;
};
virtual ~EventManager() = default;
virtual void registerEventListener(EventListener* listener) = 0;
};
class EventCounter :
// private inheritance as it's an implementation detail and this EventCounter
// should be free to refactor (e.g. move the listener implementation to a separate class)
private EventManager::EventListener {
public:
explicit EventCounter(EventManager& eventManager) {
eventManager.registerEventListener(this);
}
private:
void onEvent() override {
++eventCount;
}
public:
int eventCount = 0;
};
class MockEventManager : public EventManager {
public:
MOCK_METHOD1(registerEventListener, void(EventListener*));
};
TEST(EventCounterTest, eventDispatch_incrementsCounter) {
MockEventManager eventManager;
EventManager::EventListener* listener = nullptr;
EXPECT_CALL(eventManager, registerEventListener(testing::_))
.WillOnce(testing::SaveArg<0>(&listener));
EventCounter eventCounter(eventManager);
EXPECT_EQ(0, eventCounter.eventCount);
listener->onEvent();
EXPECT_EQ(1, eventCounter.eventCount);
}
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class EventManager {
public:
class EventListener {
public:
virtual ~EventListener() = default;
virtual void onEvent() = 0;
};
virtual ~EventManager() = default;
virtual void registerEventListener(EventListener* listener) = 0;
};
class EventCounter :
// private inheritance as it's an implementation detail and this EventCounter
// should be free to refactor (e.g. move the listener implementation to a separate class)
private EventManager::EventListener {
public:
explicit EventCounter(EventManager& eventManager) {
eventManager.registerEventListener(this);
}
private:
void onEvent() override {
++eventCount;
}
public:
int eventCount = 0;
};
class MockEventManager : public EventManager {
public:
MOCK_METHOD1(registerEventListener, void(EventListener*));
};
TEST(EventCounterTest, eventDispatch_incrementsCounter) {
MockEventManager eventManager;
EventManager::EventListener* listener = nullptr;
EXPECT_CALL(eventManager, registerEventListener(testing::_))
.WillOnce(testing::SaveArg<0>(&listener));
EventCounter eventCounter(eventManager);
ASSERT_NE(listener, nullptr);
EXPECT_EQ(0, eventCounter.eventCount);
listener->onEvent();
EXPECT_EQ(1, eventCounter.eventCount);
}
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class EventManager {
public:
class EventListener {
public:
virtual ~EventListener() = default;
virtual void onEvent() = 0;
};
virtual ~EventManager() = default;
virtual void registerEventListener(EventListener* listener) = 0;
};
class EventCounter :
// private inheritance as it's an implementation detail and this EventCounter
// should be free to refactor (e.g. move the listener implementation to a separate class)
private EventManager::EventListener {
public:
explicit EventCounter(EventManager& /*eventManager*/) {
// eventManager.registerEventListener(this);
}
private:
void onEvent() override {
++eventCount;
}
public:
int eventCount = 0;
};
class MockEventManager : public EventManager {
public:
MOCK_METHOD1(registerEventListener, void(EventListener*));
};
TEST(EventCounterTest, eventDispatch_incrementsCounter) {
MockEventManager eventManager;
EventManager::EventListener* listener = nullptr;
EXPECT_CALL(eventManager, registerEventListener(testing::_))
.WillOnce(testing::SaveArg<0>(&listener));
EventCounter eventCounter(eventManager);
EXPECT_EQ(0, eventCounter.eventCount);
listener->onEvent();
EXPECT_EQ(1, eventCounter.eventCount);
}