Note that there are some explanatory texts on larger screens.

plurals
  1. POCan a mock be reused for multiple expected calls with different return values in Hippo Mocks?
    primarykey
    data
    text
    <p>I'm using Hippo Mocks to great success, but I have a situation that I can't quite figure out how to set up properly. The code under test looks sorta like this:</p> <pre><code>auto firstName = record.at("firstName").getValue(); auto lastName = record.at("lastName").getValue(); </code></pre> <p>Where <code>IRecord::at()</code> returns a <code>IColumn&amp;</code>, which has a pure virtual <code>getValue()</code> method. I'm trying to test my code with mocked versions of <code>Irecord</code> and <code>IColumn</code>:</p> <pre><code>auto mockRec = mocks.InterfaceMock&lt;IRecord&gt;(); auto mockCol = mocks.InterfaceMock&lt;IColumn&gt;(); </code></pre> <p>I can set up the expectations for <code>firstName</code> just fine:</p> <pre><code>mocks.OnCall(mockRec, IRecord::at).With("firstName").Return(std::ref(*mockCol)); mocks.OnCall(mockCol, IColumn::getValue).Return(std::string("John"))); </code></pre> <p>But I would like to reuse the <code>IColumn</code> mock for the next expectation for <code>lastName</code>.</p> <pre><code>mocks.OnCall(mockRec, IRecord::at).With("lastName").Return(std::ref(*mockCol)); mocks.OnCall(mockCol, IColumn::getValue).Return(std::string("Doe"))); </code></pre> <p>But when I run this, Hippo Mocks returns "John" for both <code>getValue()</code> calls.</p> <p>Following along with the <a href="https://www.assembla.com/spaces/hippomocks/wiki/Tutorial_3_0" rel="nofollow">tutorial</a>, I tried to restrict the order of "firstName" and "lastName" calls:</p> <pre><code>auto&amp; firstCall = mocks.OnCall(mockRec, IRecord::at).With("firstName").Return(std::ref(*mockCol)); mocks.OnCall(mockCol, IColumn::getValue).After(firstCall).Return(std::string("John"))); auto&amp; lastCall = mocks.OnCall(mockRec, IRecord::at).With("lastName").Return(std::ref(*mockCol)); mocks.OnCall(mockCol, IColumn::getValue).After(lastCall).Return(std::string("Doe"))); </code></pre> <p>But I still get "John" for both <code>getValue()</code> calls.</p> <p><strong>Q:</strong> Is it possible to reuse the <code>IColumn</code> interface and tell Hippo Mocks to return different values with each <code>getValue()</code> call like I need, or am I stuck creating a separate <code>IColumn</code> mock for each parameter? Note: My actual implementation will have more than two parameters, so reusing the <code>IColumn</code> mock reduces a lot of setup for each unit test.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload