uber / needle

Compile-time safe Swift dependency injection framework
Apache License 2.0
1.84k stars 144 forks source link

Needle Usage for Integration testing #462

Open ShubhangTripathi opened 1 year ago

ShubhangTripathi commented 1 year ago

Hello Team, We are looking into re-using Components for our integration testing purposes. We plan on using a separate DI hierarchy for the testing purpose, and wanted to re-use most of our regular components with minor modifications. For example:

class RootComponent: BootstrapComponent {
  var networkComponent: NetworkComponent {
   return NetworkComponent(parent: self)
  }
  .
  .
  .
}
class NetworkComponent: Component<NetworkDependency> {
  var urlSession: URLSession {
     <returns an actual URL session>
  }
 .
 .
 .
}

Now, when we want to create one for testing, we would like to have something like:

class TestNetworkComponent: NetworkComponent {
  override var urlSession: URLSession {
     <returns a mock of URLSession>
  }
}
class TestRootComponent: BootstrapComponent {
  var networkComponent: NetworkComponent {
   return TestNetworkComponent(parent: self)
  }
  .
  .
  .
}

Here, we inherit the NetworkComponent and override urlSession, but the other members are inherited. Now we can use this TestNetworkComponent as part of the testing DI hierarchy.

Currently this approach does not work, since TestNetworkComponent is not picked up by needle's code generator and we run into runtime issues.

Is this something the team might be interested in adding support for? Or any recommendations to do things in a different way?

Thanks!