mercredi 6 avril 2016

object graph for better testability

Should I refactor my system under test code to be accepting an object graph instead of each individual dependency?

Because I did not start my solution having unit testing in mind, I am refactoring for testability.

I'm injecting all of my dependencies through the constructor:

var myApp = new MyClass(DependencyOne, DependencyTwo, ...DependencyTen)

Within the constructor I am initializing my privates to those dependencies:

MyClass
{
  MyClass(IDependencyOne, IDependencyTwo, ...IDependencyTen)
  {
    dependencyOne = IDependencyOne;
    dependencyTwo = dependencyTwo ;
    ...
    dependencyTen = IDependencyTen;

  }
}

For my unit testing, I can now Mock each dependency:

var mockDep1 = new Mock<IDependencyOne>();
var mockDep2 = new Mock<IDependencyTwo>();
...
var sut = new MyClass(mockDep1.Object, mockDep2.Object..)

What is the best-standards way of structuring code that accepts a lot of dependencies to be testable?

Aucun commentaire:

Enregistrer un commentaire