0

Suppose I have similar function:

Helper2 helper2;
public void myFunc(Helper helper)
{
    if (....)
        helper.mySecondFunc(); 
    else
        helper2.myThirdFunc(); 
}

In my unit test should I assert that my SecondFunc or ThirdFunc are called once (isn't this tighly coupling and hardcoding) or just mock/stub them to do dummy things and assert if the dummy things are done?

2 Answers 2

6

It depends on what you are testing.

For pure simple helper as you have here, I may not really bother about them in my test on higher end functions, and have unit test dedicated to the helper function. But if my helper rely on some external system, I would do some mock to check that the function I want to be called is properly called: If my helper is doing some database query for example, I'd check that the query function/method is properly called, and mock its result if I want to test for specific error.

5

Test the API.

  • If Helper has tests of its own, you mock it here
  • If Helper is an internal class to this API, don't let it leak (not even to tests).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.