googlemock

Google Mock unit testing static methods c++

孤街浪徒 提交于 2020-01-19 07:46:09
问题 I just started working on unit testing (using BOOST framework for testing, but for mocks I have to use Google Mock) and I have this situation : class A { static int Method1(int a, int b){return a+b;} }; class B { static int Method2(int a, int b){ return A::Method1(a,b);} }; So, I need to create mock class A, and to make my class B not to use real Method1 from A class, but to use mock. I'm not sure how to do this, and I could not find some similar example. 回答1: You could change class B into a

How to set, in google mock, a void* argument to a set of values?

时光毁灭记忆、已成空白 提交于 2020-01-14 09:09:16
问题 I am using google mock to unit test my code, and I am trying to return, as an output argument, a set of values through a void*. uint32_t bigEndianTestValues[BIG_ENDIAN_FIELD_MAX_ELEMENTS] = {0xDEADBEEF, 0xFFFF0000, 0x00000000, 0x00A00F10, 0x11234211}; for (int i = 0; i < BIG_ENDIAN_FIELD_MAX_ELEMENTS; ++i) { EXPECT_CALL( deviceWindow, get(_,sizeof(bigEndianTestValues[0]),_,_) ) .WillOnce(SetArgPointee<2>(bigEndianTestValues[i])) .RetiresOnSaturation(); } My mock contains a method call as

How to set, in google mock, a void* argument to a set of values?

こ雲淡風輕ζ 提交于 2020-01-14 09:09:03
问题 I am using google mock to unit test my code, and I am trying to return, as an output argument, a set of values through a void*. uint32_t bigEndianTestValues[BIG_ENDIAN_FIELD_MAX_ELEMENTS] = {0xDEADBEEF, 0xFFFF0000, 0x00000000, 0x00A00F10, 0x11234211}; for (int i = 0; i < BIG_ENDIAN_FIELD_MAX_ELEMENTS; ++i) { EXPECT_CALL( deviceWindow, get(_,sizeof(bigEndianTestValues[0]),_,_) ) .WillOnce(SetArgPointee<2>(bigEndianTestValues[i])) .RetiresOnSaturation(); } My mock contains a method call as

How to set a value to void * argument of a mock method in google mock testing?

≡放荡痞女 提交于 2020-01-13 11:04:18
问题 I want to pass a string "Device Name" to a void * pointer argument of a method and retrieve it to a character array later. For this I've done as shown below. Here I have created an action to achieve this. ACTION_P(SetArg2ToChar, value) {*static_cast<char*>(arg2) = *value; } Actual method to be called/mocked bool getDictItem(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo) My mock method MOCK_METHOD5(getDictItem, bool(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD

Mocking C++ classes with dependency injection

别等时光非礼了梦想. 提交于 2020-01-11 07:06:48
问题 Say you're testing class A and it has a dependency injection of B which has a dependency injection of C . So you mock B but the only constructor it has requires an injection of C , so do you have to mock C as well and inject the mocked C into the mocked B and only then inject it to A ? What if you have 5 consecutive dependancies? What are the alternatives? I use Google Mock, so a specific answer would help as well. 回答1: Emile has the right idea, you should depend on interfaces not concrete

EXPECT_CALL of googlemock leads to “unknown file:error: SEH exception with code 0xc0000005 thrown in the test body” [closed]

自作多情 提交于 2020-01-03 20:13:29
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I am novice to googlemock. My current project needs googlemock to use. I have learned from basics of gmock from google help site. But when I have tried

EXPECT_CALL of googlemock leads to “unknown file:error: SEH exception with code 0xc0000005 thrown in the test body” [closed]

情到浓时终转凉″ 提交于 2020-01-03 20:13:23
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I am novice to googlemock. My current project needs googlemock to use. I have learned from basics of gmock from google help site. But when I have tried

Google Mock: Return() a list of values

为君一笑 提交于 2020-01-03 15:34:24
问题 Via Google Mock's Return() you can return what value will be returned once a mocked function is called. However, if a certain function is expected to be called many times, and each time you would like it to return a different predefined value. For example: EXPECT_CALL(mocked_object, aCertainFunction (_,_)) .Times(200); How do you make aCertainFunction each time return an incrementing integer? 回答1: Use sequences: using ::testing::Sequence; Sequence s1; for (int i=1; i<=20; i++) { EXPECT_CALL

How to unit test BSD sockets [closed]

空扰寡人 提交于 2020-01-01 11:53:06
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I am writing a server/client based C++ application in Ubuntu with BSD socket. I am using Google C++ Test Framework as my unit test framework. I wonder is there a way that I can create a server and client in my unit test, so I can test listen/accept for the server, and send/receive

Can I copy a google mock object after setting expectations?

笑着哭i 提交于 2019-12-24 11:27:16
问题 I want to add a utility function in my test fixture class that will return a mock with particular expectations/actions set. E.g.: class MockListener: public Listener { // Google mock method. }; class MyTest: public testing::Test { public: MockListener getSpecialListener() { MockListener special; EXPECT_CALL(special, /** Some special behaviour e.g. WillRepeatedly(Invoke( */ ); return special; } }; TEST_F(MyTest, MyTestUsingSpecialListener) { MockListener special = getSpecialListener(); // Do