Dependency injection inside Unit testing
See original GitHub issueHi,
I have two components that need to talk to each other, to avoid the circular dependency issue, I’m using this.moduleRef.get
on both components to access the instance methods.
When doing unit test with @nestjs/testing
after creating the module and declare both components, the DI doesn’t works:
Scenario 1:
- Component1 - declares dependency on Component2 via moduleRef
- Component2 - declares dependency on Component1 via moduleRef
- server behaviour: works
- unit test behaviour: fails (both c1 -> c2 & c2 -> c1)
Scenario 2:
- Component1 - declares dependency on Component2 via moduleRef
- Component2 - declares dependency on Component1 via constructor
- server behavour: works
- unit test behaviour: fails for c1 -> c2 & works for c2 -> c1
So I can’t get it working 😦
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Unit Testing and Dependency Injection - The Coders Tower
Unit testing is challenging when you add frameworks around your tests subjects. Dependency injection is one of those patterns supported by ...
Read more >Is dependency injection essential for unit testing?
Dependency injection allows unit testing, but it also allow modification of an object's behavior without altering the code of that object ...
Read more >Dependency Injection and Unit Testing - JavaRanch
As its name says, Dependency Injection is all about injecting dependencies, or simply said, setting relations between instances. Some people refer to it...
Read more >Using DI container in unit tests - Stack Overflow
We've been using constructor injection for all of our production classes, and configuring Simple Injector to populate everything, and ...
Read more >Favor real dependencies for unit testing - Stack Overflow Blog
If you've worked with unit testing, you've probably used dependency injection to be able to decouple objects and control their behavior ...
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 FreeTop 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
Top GitHub Comments
Hi, @kamilmysliwiec. Is there any plan to make the testing module call application lifecycle hooks? It would be nice to have that too. PS: Nest is awesome 👍
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.