I have a c++ function to test, and this c++ function is:
void RequestProcessor::processRequest(
SmartPointer<Request>& request,
SmartPointer<RequestContext>& context){
// business logic(inside i will call a C function, so I will mock that C function in my testing)
context->deliver(Response);
}
So I construct a request processor and a mocked context. In my mocked context, I will check if the response matches my expectation. I have multiple test cases. If i commented other tests and only run each one every time, each one passed. But if i run them together, they still passed, but after all passed, it showed "segment fault(core dumped)".
[==========] Running 3 tests from 3 test cases.
[----------] Global test environment set-up.
[----------] 1 test from Test1
[ RUN ] Test1.testSuccess
[ OK ] Test1.testSuccess (59 ms)
[----------] 1 test from Test1 (59 ms total)
[----------] 1 test from Test2
[ RUN ] Test2.
[ OK ] Test2
[----------] 1 test from Test2 (5 ms total)
[----------] 1 test from Test3
[ RUN ] Test3
[ OK ] Test3
[----------] 1 test from Test3 (11 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 3 test cases ran. (75 ms total)
[ PASSED ] 3 tests.
Segmentation Fault (core dumped)
I have a "mock.h" and "mock.cc" to define "mockClass". My .t.cpp file is:
mockClass MockObj; // global mock object to mock a C function that my testing c++ funtion will call
//mock context
template <typename RESPONSE>
class mockContext
: public RequestContext<RESPONSE> {
public:
virtual int deliver()
{
// check response here
};
TEST(Test0, testUnknownError){
EXPECT_CALL(MockObj, C-function(Str1, Str2)).WillOnce(Throw(runtime_error("testError")));
// construct mock context
mockContext<Response>* contextImp = new mockContext<Response>();
smartPointer<RequestContext> contextImp_mp(contextImp);
smartPointer<RequestContext> baseContext =
smartPointer<RequestContext>(new RequestContext(contextImp_mp);
RequestContext context(baseContext);
SmartPointer<RequestContext> context_mp(&context);
//construct request
Request req;
smartPointer<Request> request_mp(&req);
processor.processRequest(request_mp,context_mp);
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&MockObj));
Mock::AllowLeak(&MockObj);
}
TEST(Test0, test1){
EXPECT_CALL(MockObj, C-function(Str1, Str2)).WillOnce(Throw(runtime_error("testError")));
// construct mock context
mockContext<Response>* contextImp = new mockContext<Response>();
smartPointer<RequestContext> contextImp_mp(contextImp);
smartPointer<RequestContext> baseContext =
smartPointer<RequestContext>(new RequestContext(contextImp_mp);
RequestContext context(baseContext);
SmartPointer<RequestContext> context_mp(&context);
//construct request
Request req;
smartPointer<Request> request_mp(&req);
processor.processRequest(request_mp,context_mp);
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&MockObj));
Mock::AllowLeak(&MockObj);
}
TEST(Test1, test2){
EXPECT_CALL(MockObj, C-function(Str1, Str2)).WillOnce(return(1)));
// construct mock context
mockContext<Response>* contextImp = new mockContext<Response>();
smartPointer<RequestContext> contextImp_mp(contextImp);
smartPointer<RequestContext> baseContext =
smartPointer<RequestContext>(new RequestContext(contextImp_mp);
RequestContext context(baseContext);
SmartPointer<RequestContext> context_mp(&context);
//construct request
Request req;
smartPointer<Request> request_mp(&req);
processor.processRequest(request_mp,context_mp);
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&MockObj));
Mock::AllowLeak(&MockObj);
}
First I thought it's because of the global variable "mockObj", so I added "Mock::AllowLeak(&MockObj);" for each tests. But still get segment fault after passing all the tests. So I guess maybe the problem is when I tried to mock the context, i did not clean up well each time? this may cause miscommunication of ownership issues. Debugger also stops at the end of "context mocking" part. Any idea how this? Is it because of the global variable or context mocking? Thanks
Aucun commentaire:
Enregistrer un commentaire