Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the missing piece of the puzzle is that you don't have to set an expectation on a method and instead you can just set a default return value.</p> <p><strong>Mocks</strong></p> <p>All the discussion and examples in the "<a href="http://code.google.com/p/googlemock/wiki/ForDummies">Google Mock for Dummies</a>" is focused around setting expectations. Everything talks about using some code similar to the following:</p> <pre><code>EXPECT_CALL(turtle, PenDown()) .Times(AtLeast(1)); </code></pre> <p>Which is what you want for mocking, but for stubbing you don't have any expectations. After reading that intro I had no clue how to use googlemock for stubbing.</p> <p><strong>Stubs</strong></p> <p>ratkok's comment led me to find out how to set a default return value. Here's how to specify a return value for a mocked object but no expectation:</p> <pre><code>ON_CALL(foo, Sign(_)) .WillByDefault(Return(-1)); </code></pre> <p><a href="http://code.google.com/p/googlemock/wiki/CookBook#Setting_the_Default_Actions_for_a_Mock_Method">http://code.google.com/p/googlemock/wiki/CookBook#Setting_the_Default_Actions_for_a_Mock_Method</a></p> <p>It appears googlemock will issue a warning if you call a method that has no EXPECT_CALL. Apparently you can prevent this warning by using their <a href="http://code.google.com/p/googlemock/wiki/CookBook#Nice_Mocks_and_Strict_Mocks">NiceMock</a> construct or you can just ignore it. Additionally it appears you can avoid the warning by using an expect instead (which I'm not sure if it's a good idea for stubs). From the <a href="http://code.google.com/p/googlemock/wiki/FrequentlyAskedQuestions#Google_Mock_prints_a_warning_when_a_function_without_EXPECT_CALL">Google Mock FAQ</a>:</p> <pre><code>EXPECT_CALL(foo, Bar(_)) .WillRepeatedly(...); </code></pre> <p>I believe that is exactly what I was trying to figure out.</p> <p><strong>Update</strong></p> <p>I can confirm this works. I wrote a unit test using google test along with googlemock and was able to stub out a method for a class using ON_CALL.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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