shlogg · Early preview
Jetthoughts Dev @jetthoughts

Decoupling Tests From External Requests With Bridge Pattern

Decoupling abstraction from implementation with Bridge Pattern reduces mocks & stubs in tests. Use different sources for data in prod vs test envs. `Medium` class uses `PostsSource::Remote` or `PostsSource::Fake` depending on environment.

Do you have difficulties in adding the new tests and their readability decreased due to mocks and stubs? Let’s try to get rid of external requests in tests.


The main idea is to override implementation dynamically during the call of external service. In other words, to use different sources for receiving data for different environments. Suppose for production environments, you get the data from a third-party server, and for a test environment, the source can simply return an object of the desired format.

  
  
  Bridge Pattern: class with abstraction

First of all, we separate responsibiliti...