I am new to unittesting and currently have a problem with finding a decent way to test methods that contain branches.
I created a small demo method I hope could be used to explain the problem.
public void ExportAccounts()
{
int emptyAccounts = 0;
int nonEmptyAccounts = 0;
int errorous = 0;
string outputPath = this.GetOutputPath();
Account[] accounts = this.MyWebserviceAdapter.GetAccounts();
foreach(Account account in accounts)
{
try
{
if(account.Amount > 0)
{
this.ExportNonEmpty(outputPath, account);
nonEmptyAccounts++;
} else {
this.ExportEmptyAccount(outputPath, account);
emptyAccounts++;
}
} catch(Exception e) {
logger.error(e);
errorous++;
}
}
logger.debug(string.Format("{0} empty / {1} non empty / {2} errorous", emptyAccounts, nonEmptyAccounts, errorous));
}
I can mock MyWebserviceAdapter to return a predefined list of accounts. Should I input a list of accounts that are empty and nonempty in the same test or should I have separate tests?
Also my ExportNonEmpty() and ExportEmpty() methods are private but do write files to the filesystem. Should I supply a mock FileProvider so that the filesystem is not being touched?
Should I expose ExportNonEmpty() and ExportEmpty() to be able to test those separately? Those methods also contain a few if-then-else statements and can throw exceptions and whatnot.
I find if I create a test for every codepath I have copy code from one test to another - generating mocks etc. .. isnt that a bit odd?
Should I expose the counter variables as out variables to be able to verify them after calling the method?
this.GetOUtputPath() fetches values from the configuration file through ConfigurationManager which is static. Should I a) mock this out either by creating a partial mock for the class under testt and overwrite the GetOutputPath method or b) Create my own ConfigurationAdapter that can be mocked out?
I am using nunit and Rhino Mocks.