0

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;
Gionata Benelli
  • 357
  • 1
  • 3
  • 20
  • What *about it* do you want to be able to test? It's easy to replace the clock with an interface that you can mock, but to be honest this seems like one of those times that mocking is misused - and you'll end up testing your mocks (great success). – sehe Nov 05 '21 at 00:39
  • i would like to get back a predefined time from the call to local_time() since the timestamp in my message could be either the time of day or the offset from another command, this was not already implemented in my method, and I don't know how to test it unless I mock the local_time function – Gionata Benelli Nov 05 '21 at 07:41

0 Answers0