You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Decouple Dependencies to Improve Testability and Reduce Tight Coupling #3
Dependencies need to be decoupled. Since most of our dependencies are not injected through constructors, the code has tight coupling and is not testable. We can adopt a hybrid approach: if the dependency is not provided through the constructor, create it using a private factory method. This way, we can avoid bloated constructors while achieving highly testable code.
Unit tests should not directly depend on an object. Instead, we should mock all the dependencies to avoid relying on concrete objects. If one of our dependencies requires additional parameter or stops supporting a specific value in the future, we don’t want to rewrite all the tests where we used a real instance instead of a mock.
Activity
UnnikrishnanBhargavakurup commentedon Dec 13, 2024
Unit tests should not directly depend on an object. Instead, we should mock all the dependencies to avoid relying on concrete objects. If one of our dependencies requires additional parameter or stops supporting a specific value in the future, we don’t want to rewrite all the tests where we used a real instance instead of a mock.