Gmock - matching structures

后端 未结 5 1931
误落风尘
误落风尘 2021-02-07 16:10

How can I match value of an element in a union for an input argument e.g - if I mock a method with the following signatiure -

    struct SomeStruct
    {   
           


        
5条回答
  •  萌比男神i
    2021-02-07 17:08

    After reading through the Google mock documentation in detail, I solved my problem as documented in Defining Matchers section. (An example would have been great!)

    So the solution is to use the MATCHER_P macros to define a custom matcher. So for the matching SomeStruct.data1 I defined a matcher:

    MATCHER_P(data1AreEqual, ,"") { return (arg.data1 == SomeStructToCompare.data1); }
    

    to match it in an expectation I used this custom macro like this:

    EXPECT_CALL(someMock, SomeMethod(data1AreEqual(expectedSomeStruct)));
    

    Here, expectedSomeStruct is the value of the structure.data1 we are expecting.

    Note that, as suggested in other answers (in this post and others), it requires the unit under test to change to make it testable. That should not be necessary! E.g. overloading.

提交回复
热议问题