1

Many Mock frameworks has a feature to verify if a method is called or not. However most frameworks requires that the code follows dependency injection pattern.

The code that I'm trying to test does NOT uses dependency injection pattern, therefore a mock of the object can not be injected.

Code ex.:

public class TestMeClass {
    public void TransformMe() { }
}

public abstract class SomeeClass {
    public SomeMethod() {
        CallMeMethod();
    }

    private void CallMeMethod() {
        TestMeClass testMeClass = new TestMeClass();
        testMeClass.TransformMe();
    }
}

How can I verify(unit test) that TransformMe() is called?

Can it be done using reflections`? (Language is C#)

Jon Skeet I need you.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
RayOldProf
  • 1,040
  • 4
  • 16
  • 40
  • That method is difficult to test as the dependency is being initialized within the parent class itself. – Nkosi Nov 24 '16 at 15:43
  • This really isn't possible. If you want to test your SomeeClass Logic is behaving as it should, you're going to need to inject an instance of TestMeClass somehow so that you can spy on its methods. – AJ X. Nov 24 '16 at 15:44
  • I guess if you use `mock` library you could use `verify` method. look at [this](http://stackoverflow.com/questions/9841623/mockito-how-to-verify-method-was-called-on-an-object-created-within-a-method) – esiprogrammer Nov 24 '16 at 15:48
  • If you only need to test it once, I would use a File.Write(...) to check if the method is called. If you need a unit test I would suggest that you do a refactoring. – Hans-Rudolf Steiger Nov 24 '16 at 15:49
  • @esiprogrammer Negative – RayOldProf Nov 24 '16 at 15:49
  • I'd definitely like to see where you're going with this. Will something like this help you out? http://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method – AJ X. Nov 24 '16 at 15:52
  • `TestMeClass ` is completely encapsulated within a private method. which means nothing external to that class or method knows anything about it. it is not testable. – Nkosi Nov 24 '16 at 15:55

1 Answers1

3

How can I verify(unit test) that TransformMe() is called?

No. Not without exposing something public that can be verified.

Really Bad example just to make a point.

public abstract class SomeeClass{
   public SomeMethod(){
    CallMeMethod();
   }

   public bool TransformMeCalled {get;private set;}

   private void CallMeMethod() {
      TestMeClass testMeClass = new TestMeClass();
      testMeClass.TransformMe();
      TransformMeCalled = true;
   }
}

Can it be done using reflections`? (Language is C#)

Again No. The dependency is being created internally in the dependent class and is unknown to external actors. An external access point needs to be available otherwise this is not testable. You answered the question when you said

The code that i'm trying to test does NOT uses dependency injection pattern, therefore a mock of the object can not be injected.

One option is to refactor the creation of the dependency which would allow for it to be overridden, but again it is something that has to be exposed to an external actor to be accessed

public abstract class SomeeClass{
    public SomeMethod(){
        CallMeMethod();
    }

    public abstract TestMeClass CreateTestMeClass();

    private void CallMeMethod() {
        TestMeClass testMeClass = CreateTestMeClass();
        testMeClass.TransformMe();
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472