Testing singletons
See original GitHub issueI’m writing tests against a class marked @singleton()
. In my beforeEach
I resolve the class, but since it’s a singleton I get the same instance for each test. Ideally each test would be independent of the others. I tried calling container.reset()
but that wipes out all of the registered types. Is there room in this library for a “soft” reset, or is there another approach I could take?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9
Top Results From Across the Web
Unit testing with singletons - Stack Overflow
To run code containing singletons in a test harness, we have to relax the singleton property. Here's how we do it. The first...
Read more >Don´t use Singleton Pattern in your unit tests
Singleton classes do not allow for Test Driven Development (TDD) The laws of TDD. Unit testing depends on tests being independent of one...
Read more >Unit Testing a Singleton in C++
The difficulty that we encounter when attempting to test a Singleton, is that quite often they are created as global variables at startup....
Read more >Avoid Singletons to Write Testable Code - CodeAhoy
A look at the singleton design pattern in Java and why they make unit testing difficult with examples. Also explores alternate strategy.
Read more >Singletons The Safe Way - Matt Carroll
That's the Singleton pattern. The Danger of Singletons. Can something as simple as a Singleton pose a threat to code health? Absolutely. The...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello, I’ve the same problem. There is a way to reset only the instances of registered singletons?
Just a heads up. You can do this cleanly with the new ContainerScoped lifecycle introduced on version 4. ContainerScoped behaves pretty much like a singleton, except the cached instance is reset for every new child container created.