I'm having some issue trying to test some fuctionalities of a cpp class. I'm testing my class, let's call it myManager, which has a public method called generateActionCommand that uses boost posix_time function local_time() to return a ptime structure in order to generate a command with the format "timestamp,id,CMD_TYPE" where:
- timestamp is a string representing the time of the machine as hh:mm:ss.ms;
- id is an integer
- CMD_TYPE, is a string, e.g., "START"
Now, I'm testing myManager using boost.test and turtle-mock and I have wrote already some unit testing and used mocking in order to emulate other classes, but how can I test that the timestamp is generated correctly?(Ican test that the function respects the format, by using regex but I have no guarantees on the time returned. I would like to mock the function using MOCK_FUNCTION, but it does not seem to work.
Below, the code for the method I'm trying to test:
std::string OEManager::generateActionCommand(const char* cmd_type) {
std::string ret_msg;
pt::ptime current_time = pt::microsec_clock::local_time();
char timestamp[13];
//NOTE: NO us(microseconds) support on windows machine time in format (hh:mm:ss.ms)
snprintf(timestamp, 13, "%02d:%02d:%02d.%03d",
current_time.time_of_day().hours(), current_time.time_of_day().minutes(),
current_time.time_of_day().seconds(), current_time.time_of_day().fractional_seconds());
ret_msg = timestamp;
ret_msg.append ("," + std::to_string(sequence_id) + "," + cmd_type);
return ret_msg;