Singleton testing throwing compilation errors with Catch2
Closed this issue · 2 comments
stiggy87 commented
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
Since your base class has a private constructor, your mock cannot be created. If you make the base class' constructor protected
it works fine.
stiggy87 commented
Great! Thanks for the quick response!