title: Using GoogleMock to avoid dependencies when testing a Card and a CardCollection class
body: <p>I'm learning about mocking and am reasonably new to the concepts. For the sake of learning, I'm implementing a <code>Card</code> class and a <code>CardCollection</code> class which depends on the <code>Card</code>. Therefore I've created a <code>MockCard</code> for testing the <code>CardCollection</code>.</p>
  <p>Here's the <code>Card.h</code></p>
  <pre><code>    class ICard {
      protected:
          int rank_;
          std::string suit_;
      public:
  
          ICard(int rank, std::string suit) : rank_(rank), suit_(std::move(suit)) {};
  
          [[nodiscard]] virtual int getRank() const = 0;
  
          [[nodiscard]] virtual std::string getSuit() const  = 0;
  
          friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const ICard &amp;c);
  
          bool operator&gt;(const ICard &amp;other) const;
  
          bool operator&lt;(const ICard &amp;other) const;
  
          bool operator&lt;=(const ICard &amp;other) const;
  
          bool operator&gt;=(const ICard &amp;other) const;
  
          bool operator==(const ICard &amp;other) const;
  
          bool operator!=(const ICard &amp;other) const;
      };
  
      class Card : public ICard {
      public:
  
          Card(int r, std::string s);
  
          [[nodiscard]] int getRank() const override;
  
          [[nodiscard]] std::string getSuit() const override;
  
      };
  
  </code></pre>
  <p>The <code>MockCard.h</code></p>
  <pre><code>#include &quot;gmock/gmock.h&quot;
  class MockCard : public ICard {
  public:
      MockCard(int rank, std::string suit) : ICard(rank, std::move(suit)){};
      MOCK_METHOD(int, getRank, (), (const, override));
      MOCK_METHOD(std::string, getSuit, (), (const, override));
  };
  
  </code></pre>
  <p>And  <code>CardCollection.h</code>:</p>
  <pre><code>    #include &quot;Card.h&quot;
      class CardCollection {
  
      protected:
          vector&lt;ICard *&gt; cards_;
      public:
  
          CardCollection() = default;
  
          ~CardCollection() = default;
  
          explicit CardCollection(vector&lt;ICard*&gt; cards);
  
          ICard* pop();
  
      ...
  </code></pre>
  <p>Here is my test:</p>
  <pre><code>#include &quot;gmock/gmock.h&quot;
  TEST(CardCollectionTests, TestPop) {
      MockCard card1(6, &quot;C&quot;);
      std::vector&lt;ICard*&gt; cards({&amp;card1});
      CardCollection cc(cards);
  
      // pop the card off the collection, cast to MockCard
      auto* new_card = (MockCard*) cc.pop();
  
      ASSERT_EQ(6, new_card-&gt;getRank());
  }
  </code></pre>
  <p>The best test that I can think of is to verify that the Card is a 6 of clubs. But we can't use the <code>Card</code> class directly as this would introduce unwanted dependencies into the unit test. So I resolve to cast the <code>ICard*</code> returned from <code>CardCollection::pop</code> to a <code>MockCard*</code> type so that we can use the <code>getRank()</code> method and test that the value returned is a <code>6</code>. However, this is a <code>MockCard</code> not a <code>Card</code> so <code>getRank()</code> actually returns a <code>0</code> and the test fails.</p>
  <p>How can I set the value returned by <code>MockCard::getRank()</code> to <code>6</code>? More generally, Are there any obvious pitfalls you can see in my approach to testing this behavior and if so, what would you implement as an alternative?</p>