Home > OS >  GoogleMock trying to set a function argument to a specific value using EXPECT_CALL
GoogleMock trying to set a function argument to a specific value using EXPECT_CALL

Time:07-16

I have the following function prototype

std::int16_t Driver::ListDevices( struct BoardInfo devInfo[], size_t len, int* pCount )

struct BoardInfo
{
    int iBoardNum;
    WORD wSlot;
    char cSite;
};

I have created a Mock for it as follows

MOCK_METHOD3( ListDevices, std::int16_t( struct BoardInfo devInfo[], size_t   len, int* pCount ) );

When I call the function using my mock object I want it to return the value 0 and to set the value pointed to by the third parameter pCount to the value 1

The following partial solution successfully compiles and returns the value 0 when the EXPECT_CALL gets invoked

EXPECT_CALL( *m_pMockObject, ListDevices( testing::_, testing::_, testing::_ ) ).Times( 1 ).WillOnce( testing::Return( 0 ) );

However if add to it in an attempt set the 3rd parameter to value 1 with the following

EXPECT_CALL( *m_pMockObject, ListDevices( testing::_, testing::_, testing::_ ) ).WillOnce( testing::SetArgPointee<2>( 1 ) ).Times( 1 ).WillOnce( testing::Return( 0 ) );

It no longer compiles giving the following errors

1>------ Build started: Project: Test_Units_TTVC_MPU_ControlPad, Configuration: UnitTest_Debug x64 ------
1>  UnitTestTTVC_MPU_ControlPad.cpp
1>c:\workspace\mpu\controlpad-per-994\libs\googlemock\v1_7_0\bin\include\gmock\gmock-actions.h(446): error C2440: 'return': cannot convert from 'void' to 'short'
1>  c:\workspace\mpu\controlpad-per-994\libs\googlemock\v1_7_0\bin\include\gmock\gmock-actions.h(446): note: Expressions of type void cannot be converted to other types
1>  c:\workspace\mpu\controlpad-per-994\libs\googlemock\v1_7_0\bin\include\gmock\gmock-actions.h(445): note: while compiling class template member function 'short testing::PolymorphicAction<testing::internal::SetArgumentPointeeAction<2,int,false>>::MonomorphicImpl<F>::Perform(const std::tuple<A1,std::A2,A3> &)'
1>          with
1>          [
1>              F=int16_t (BoardInfo *,std::size_t,int *),
1>              A1=BoardInfo *,
1>              A2=std::size_t,
1>              A3=int *
1>          ]
1>  c:\workspace\mpu\controlpad-per-994\libs\googlemock\v1_7_0\bin\include\gmock\gmock-actions.h(433): note: see reference to class template instantiation 'testing::PolymorphicAction<testing::internal::SetArgumentPointeeAction<2,int,false>>::MonomorphicImpl<F>' being compiled
1>          with
1>          [
1>              F=int16_t (BoardInfo *,std::size_t,int *)
1>          ]
1>  c:\workspace\mpu\controlpad-per-994\code\src_unit_test\test_units_ttvc_mpu_controlpad\unittestttvc_mpu_controlpad.cpp(142): note: see reference to function template instantiation 'testing::PolymorphicAction<testing::internal::SetArgumentPointeeAction<2,int,false>>::operator testing::Action<F>(void) const<F>' being compiled
1>          with
1>          [
1>              F=int16_t (BoardInfo *,std::size_t,int *)
1>          ]
1>  c:\workspace\mpu\controlpad-per-994\code\src_unit_test\test_units_ttvc_mpu_controlpad\unittestttvc_mpu_controlpad.cpp(142): note: see reference to function template instantiation 'testing::PolymorphicAction<testing::internal::SetArgumentPointeeAction<2,int,false>>::operator testing::Action<F>(void) const<F>' being compiled
1>          with
1>          [
1>              F=int16_t (BoardInfo *,std::size_t,int *)
1>          ]
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

I'm not sure what I'm doing wrong any advice greatly appreciated

CodePudding user response:

Your actions are not combined properly.

EXPECT_CALL( *m_pMockObject, ListDevices( testing::_, testing::_, testing::_ ) )
  .WillOnce(testing::DoAll(testing::SetArgPointee<2>( 1 ), testing::Return( 0 )));
  • Related