c++ - How to solve this Object Slicing issue with GoogleMock -


below 1 of line in method. here have mock method "findchild" , make "chino::mock_button" instance assigned "close_button". requirement.

 chino::button* close_button = findchild<chino::button>("closebutton"); 

methods tried:

  1. since findchild template, can't mock it. changed implementation of findchild template specialize chino::button type , mock new function "getchinobuttoninstance(qstring,bool)" , make return chino::mock_button instance rather chino::button instance.

    template<> inline chino::button* mediator::findchild<chino::button>(const qstring &name, bool recursive) {    return getchinobuttoninstance(name,recursive);    } 

then, in unittestclass have mocked "getchinobuttoninstance".

 mock_method2(getchinobuttoninstance,chino::mock_button*(qstring,bool)); 

and expect_call :

expect_call(*wlighting,getchinobuttoninstance("a",true)).times(testing::atleast(1)).willonce(testing::returnpointee(&wlighting->sourcebuttonmock)); 

here instead of returnpointee, have tried return , returnref. in cases findchild assigns "close_button" nullptr.

  1. second method:

we have separate store class created. put , values.here before calling findchild method, storing chino::mockbutton instance in store class. findchild class modified :

template<> inline chino::button* mediator::findchild<chino::button>(const qstring &name, bool recursive) {   harmanuteststore *store=harmanuteststore::instance();   chino::mock_button *val=dynamic_cast<chino::mock_button*>(store->getmockinstance());   return val; } 

here problem object slicing. can see debugger showing "chino::mockbutton" instance being returned when "store->getmockinstance()" called. not sure problem is, debugger not showing value of "val" , directly stepping findchild statement , having object of chino::button instance assigned "close_button" instead of chino::mockbutton instance. sure problem object slicing dont know how solve issue.

i doing wrong casting in second approach.

chino::button *val=static_cast<chino::mock_button*>(store->get(key_chino_close_button_instance)); 

this fixed issue.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -