Reputation: 11331
I am writing a piece of simple C++ class and trying to have a unittest case for the code.
The code is as simple as:
class Foo
{
static int EntryFunction(bool flag)
{
if(flag)
{
TryDownload();
}
else
{
TryDeleteFile();
}
return 0;
}
static void TryDownload()
{
// http download code
}
static void TryDeleteFile()
{
// delete file code
}
}
The issue is, according to the concept of UT, we cannot relay on the network connection. So the unittest cannot really run the download code. My ultimate goal is just to test the code path, for example: when pass in TRUE
, the download code path should hit, otherwise the delete logic should hit. I was thinking to override this class so download and delete function can be override to just set a flag and noop, but the functions are static.
I was wondering in this case what will be a good way to test it?
Upvotes: 1
Views: 68
Reputation: 594
I think it depends on what is in your TryDownload and TryDelete functions. If they use some other objects/functions to perform their tasks, you can configure simulations of those objects so that your TryDownload and TryDelete don't know their aren't really downloading/deleting anything.
If you don't have such objects/functions (and everything is contained in TryDownload/TryDelete), one might argue that the code is not well suited to unit testing because it can't be broken down into small units. In that case, your only option is an actual web service (perhaps running on the localhost) that lets those functions do their thing.
Upvotes: 1
Reputation: 3640
One of the ways which I can suggest is to use Google Mock library in your unit test framework.
Using Google mock library, you can do exactly what you have explained.
Upvotes: 0