title: How to set a value to void * argument of a mock method in google mock testing?
body: <p>I want to pass a string "Device Name" to a <code>void *</code> pointer argument of a method and retrieve it to a character array later.</p>
  
  <p>For this I've done as shown below.</p>
  
  <p>Here I have created an action to achieve this.</p>
  
  <pre><code>ACTION_P(SetArg2ToChar, value) {*static_cast&lt;char*&gt;(arg2) = *value; }
  </code></pre>
  
  <p>Actual method to be called/mocked</p>
  
  <pre><code>bool getDictItem(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo&amp; sdo)
  </code></pre>
  
  <p>My mock method </p>
  
  <pre><code>  MOCK_METHOD5(getDictItem,
    bool(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo&amp; sdo));
  </code></pre>
  
  <p>in code it is called as </p>
  
  <pre><code>if( !can.getDictItem(wIndex, bSubIndex, pObjData, dwLength, tSdo) )
  </code></pre>
  
  <p>I want to pass a string to this <code>pObjData</code> (3rd argument in the list).</p>
  
  <p>In my google tests, I'm doing like this.</p>
  
  <pre><code>char szDeviceName[30]= {0};
  snprintf(szDeviceName, sizeof(szDeviceName), "%s", "Device Name" );
  
  EXPECT_CALL( mockCan, getDictItem(_,_,_,_,_) )
              .WillOnce(DoAll(SetArg2ToChar(szDeviceName),
              Return(true)))
              .RetiresOnSaturation();
  
  /*    Call a real method within which this mock method is called */
  </code></pre>
  
  <p>If I try to set this argument(pObjData) using "SetArgPointee&lt;2>" directly, I get the below error.</p>
  
  <blockquote>
    <p><strong><em>error: 'void</em>' is not a pointer-to-object type*</strong></p>
  </blockquote>
  
  <p>Hence I'm trying with <code>ACTION_P</code></p>
  
  <p>Now with this implementation, I only get the first letter of the <code>szDeviceName</code> variable (into this <code>pObjData</code>) i.e., "D" followed by 29 0's in the real code flow after this mock object is called. </p>
  
  <p>I want to get the full string name set into this <code>void *</code> arguement. </p>
  
  <p>I refered to this below question and was able progress this far. But I'm not able to pass the full string. <a href="https://stackoverflow.com/questions/17845224/how-to-set-in-google-mock-a-void-argument-to-a-set-of-values">How to set, in google mock, a void* argument to a set of values?</a></p>
  
  <p>Any information regarding this will be helpful.</p>