UnitTests: how to test dependent methods where mockery is not feasible?

I am new to UnitTesting and just ran into a situation, I don’t know how to handle, and I would appreciate any tips :)

The situation is this, imagine two methods:

// simple standalone method
public bool HelperMethod(string substr) {
    return substr.Equals("abc");
}

// complex method making (multiple) use of HelperMethod
public bool ActualMethod(string str) {

    for (var i=0; i<str.Length; i++) {
         var substr = str.Substring(i, 3);
         if (HelperMethod(substr))
             return true;
    }

    return false;
}

HelperMethod functions without dependencies, ActualMethod depends on HelperMethod, so its UnitTest will fail if HelperMethod is one.

In fact, if I'm not mistaken, this is where a mockery of drugs should come to the rescue.

( ) ( , ActualMethod ). HelperMethod ActualMethod, HelperMethod , ( , HelperMethod ..).

: , ? ? (, Assert (HelperMethodPassed)? ?

, HelperMethod ActualMethod , .

, !:)

PS: , , #, MSTest ( , ).

: HelperMethod ActualMethod .

+4
3

, , .

HelperMethod , , , , ActualMethod . , ActualMethod , HelperMethod , .

, , HelperMethod !

, HelperMethod , , HelperMethod , , (, ActualMethod). , HelperMethod , , , .

+6

MethodHelper , , , ActualMethod, ActualMethod. ActualMethod.

+4

:

  • HelperMethod, ActualMethod .
  • Then suppose that he HelperMethodworks and does not scoff at him, but uses the actual implementation, if you can guarantee that there will be no side effects.

If the tests for HelperMethodfail, you can consider everything that ActualMethodperforms as undefined behavior, so it doesn't matter if these tests pass or fail.

0
source

All Articles