Dienstag, 5. Juni 2012

Unit Testing and static methods

Miško knew since 2008 what I stumbled about today: you have a hard time if you try to test things using static classes / members.

My case was similiar to this one:

public static class SomeFactory
{
  public ISomeObject Create() { // do nasty things such as service calls }
}

public class ThisIsWhatIWantToTest
{
  public void AMethod()
  {
    // ...
    var someObject = SomeFactory.Create();
    // ...
  }
}


I wanted to write tests for AMethod but I had a hard time. Acutally I could not manage to find a seam to work around the service call easily.

Another thing I noticed along the way is, that static things obscure the dependencies in the code. This is not a good thing because it makes the code harder to understand. If you have dependencies, the should be widely visible for the sake of clarity.

I will very carefully consider the use of statics from now.

Cheers,
Tobias.

1 Kommentar:

  1. Der Kommentar wurde von einem Blog-Administrator entfernt.

    AntwortenLöschen