What Dependencies??

As a smoker, allow me to tell you this, Dependencies are NOT cool! If someone had offered me an easy way to cut my dependency on cigarettes I’d jump on it.

Sadly for me, there is no easy way to do that, but luckily for you, in some cases dropping down dependencies is possible, it’s easy and it even has an added value, at least when it comes to unit testing.

Consider this simple case:

   1: public class MyType 

   2: {

   3:   private Dependency dep; 

   4:  

   5:   public MyType(Dependency inDep) 

   6:   { 

   7:       dep = inDep; 

   8:   }

   9:  

  10:   public int Sum(int a, int b) 

  11:   {     

  12:       return dep.Sum(a, b); 

  13:   } 

  14: }

  15:  

  16:  

  17: public class Dependency 

  18: {

  19:   public int Sum(int a, int b) 

  20:   { 

  21:       return a + b; 

  22:   }

  23: } 

 



Lets say that you need to instantiate MyType for your test.In order to do so you first need to have an instance or a fake of Dependency class (as required by MyType’s Ctor ).

As stated earlier, this example is a simple one. In reality, there would be more than one dependency, and each dependency is most likely dependent on other classes itself. The plot gets thicker.

OK, back to our case. What you would normally do in order to test MyType is first deal with the types it depends on.

Faking Dependency and passing it to MyType’s constructor will do the trick,

Dependency dp = Isolate.Fake.Instance<Dependency>();

MyType mt = new MyType(dp);

Sure it seems harmless, but don’t forget that in reality, where the types are really complex and writing surplus code will really cost you time (and someone else’s real money), it wouldn’t take long to decide to skip this test case.

So let me introduce you to an easy way of handling dependencies, it’s called Isolate.Fake.Dependencies<>();

Using this new API you can Instantiate classes without being worried about their dependencies.

MyType mt = Isolate.Fake.Dependencies<MyType>();

Is all you have to do in order to Instantiate MyType. That easy!

A little peek behind scenes. What actually happens is that Isolator recognizes the types of the constructor argument and acts as follows:

- If a type is a value type – its default value is set.

- If a type is a reference type – it is being recursively faked.

Isolate.Fake.Dependencies calls the constructor which takes the most arguments, prepares the fakes, and returns the desired instance. It can match the arguments that you pass to the constructor by types.

One might ask: “But what if I want to change the behavior of one of the dependencies?”.

There are two ways of doing it:

The first one is faking the type and passing as an argument:

   1: Dependency fakeDep = Isolate.Fake.Instance<Dependency>(); 

   2: Isolate.WhenCalled(() => fakeDep.Sum(0, 0)).WillReturn(0); 

   3: MyType mf = Isolate.Fake.Dependencies<MyType>(fakeDep);

The second way is to get the Dependency out of your instance:

   1: var real = Isolate.Fake.Dependencies<MyType>(); 

   2: var fake = Isolate.GetFake<Dependency>(real); 

   3: Isolate.WhenCalled( () => fake.Sum(0,0) ).WillReturn(2); 

*NOTE that Isolate.Fake.Dependencies can’t handle ambiguity issues so you have to be explicit.

There is only one thing left and it is the added value of Isolate.Fake.Dependencies<>();

It’s always nice to have our job done for us, but there are other benefits in using this method. The main benefit is that you tests become much more robust. Lets say that tomorrow you’ll decide to change the constructor of your class. If Isolate.Fake.Instance<>() is used to instantiate objects, your tests won’t break.

Back to our example, if we change our Ctor to:

   1: public MyType(Dependency inDep , int num) 

   2: { 

   3:     dep = inDep; 

   4: } 


 

We won’t have to change your test:

   1: MyType mt = Isolate.Fake.Dependencies<MyType>();

TOP