From 20ecc3f0d04dcb2bb565db32799474c8218eea70 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 8 Apr 2018 18:40:40 +0200 Subject: [PATCH] DI: allow to trigger the lazy instantiation of a mock service instance directly Basically the mocking mechanism just switches the configuration and then waits for the service to be accessed in order to cause acutual instantiation of the mock service implementation. But sometimes we want to prepare and rig the mock instance prior to the first invocation; in such cases it can be handy just to trigger the lazy creating process --- src/lib/depend-inject.hpp | 13 +++++++++++-- tests/gui/ctrl/element-access-test.cpp | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib/depend-inject.hpp b/src/lib/depend-inject.hpp index 2cc05e43a..a7a99ed43 100644 --- a/src/lib/depend-inject.hpp +++ b/src/lib/depend-inject.hpp @@ -324,17 +324,26 @@ namespace lib { return bool(mock_); } + /** trigger lazy service object instantiation */ + MOC& + triggerCreate() + { + Depend{}.operator()(); + ENSURE (mock_); + return *mock_; + } + MOC& operator* () const { - ENSURE (mock_); + REQUIRE (mock_); return *mock_; } MOC* operator-> () const { - ENSURE (mock_); + REQUIRE (mock_); return mock_.get(); } }; diff --git a/tests/gui/ctrl/element-access-test.cpp b/tests/gui/ctrl/element-access-test.cpp index 1cb9fbf93..96f273c24 100644 --- a/tests/gui/ctrl/element-access-test.cpp +++ b/tests/gui/ctrl/element-access-test.cpp @@ -116,6 +116,7 @@ namespace test { auto location = UICoord{"win-1","persp-A","thePanel","someView","tab#5"}; DummyTab dummyTab; + fakeDirectory.triggerCreate(); fakeDirectory->expectedQuery = location; fakeDirectory->expectedAnswer = &dummyTab;