Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c++ - How to gmock cpp std functions

I'm testing my code with gtest. In my code I'm using std::thread and I need to test the situation of exception.

How can I mock std functions?

I can't separate my test for two executables and I need some test to actually create std::thread and to have few tests that test the situation of exception.

Code:

[do someting]
try {
    ChallengeThread = std::move(std::thread(&ChallengeListener, this));
    res = SUCCESS;
  } catch (const std::system_error &err) {
    Log("Caught system_error with error: " + std::string(err.what()) + ". Fail to create ChallengeListener thread");
    res = ERR_CREATE_THREAD;
  } catch (...) {
    mLogger.LogMessage("Caught ellipsis, Fail to create ChallengeListener thread");
    res = ERR_CREATE_THREAD;
  }
[do someting else] 

Test:

EXPECT_CALL(???, thread(_, _)).Times(1).WillOnce(Throw(exception));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...