rollbear / trompeloeil

Header only C++14 mocking framework
Boost Software License 1.0
802 stars 85 forks source link

Singleton testing throwing compilation errors with Catch2 #289

Closed stiggy87 closed 1 year ago

stiggy87 commented 1 year ago

I'm trying to mock a singleton class, and following the suggestion here: #196, I am getting this:

<source>: In function 'int main()':
<source>:22:19: error: use of deleted function 'MockSingleton::MockSingleton()'
   22 |     MockSingleton _mock;
      |                   ^~~~~
<source>:16:7: note: 'MockSingleton::MockSingleton()' is implicitly deleted because the default definition would be ill-formed:
   16 | class MockSingleton : public Singleton {
      |       ^~~~~~~~~~~~~
<source>:16:7: error: 'Singleton::Singleton()' is private within this context
<source>:10:18: note: declared private here
   10 |         explicit Singleton() { text = "Hello World!"; };
      |                  ^~~~~~~~~
Compiler returned: 1

https://godbolt.org/z/4qzcorbGn

Not exactly sure if I have formed things correctly, or I'm missing something more.

I guess this issue is more about adding documentation specific to singletons rather than change anything in the code.

rollbear commented 1 year ago

Since your base class has a private constructor, your mock cannot be created. If you make the base class' constructor protected it works fine.

https://godbolt.org/z/xchzq4xbY

stiggy87 commented 1 year ago

Great! Thanks for the quick response!