7 ms·
Interfaces have a different value, they make it easier to build testing facilities for unit tests as well. This works in many languages, Java, traits in Rust, e
by bluejekyll 3y ago
Interfaces have a different value, they make it easier to build testing facilities for unit tests as well. This works in many languages, Java, traits in Rust, etc.
- rf15 3y agoIs it really a good idea to bend your code to satisfy your testing environment and not the other way around?
- rtheunissen 3y ago"Bend" is not a good description of the intention here. Good code should be easy to test effectively and interfaces are a common way to achieve that. We should absolutely design our code with testing in mind. For example, you have a repository for some entity as a dependency of a service and you would like to test some behavior of the service when the repository fails. In this case, the service can depend on an interface of the repository instead and the test can create a stub that implements that interface, whereby you can simulate the error. If the service depended on the actual repository, this becomes very difficult to do.
- agalunar 3y ago> Good code should be easy to test effectively Can you explain why this is? or maybe more specifically what you mean by this.
- robertlagrant 3y agoYour tests are part of your code. Your implementation code should not make writing your test code difficult.
- pjc50 3y agoIt's kind of unavoidable once you're trying to do unit tests with mocks. You either end up with interfaces for everything and a dependency injection framework, or you end up with some very non-isolated tests that take ages to run because they need inseparable parts of the whole real system.