title: How can I mock object that is stored as a unique_ptr in gmock?
body: <p>I inject a dependency in some class. This class stores the dependency with an <code>std::unique_ptr</code> and is therefore the only owner of the object.</p>
  
  <p>What is the right way to mock a method in this dependency? My current solution is to get a raw pointer from the <code>unique_ptr</code> before I hand over ownership. While this works, I think there are better ways to do it. What are they?</p>
  
  <pre><code>class Dependency
  {
  public:
      virtual int plusOne(int x) {return x+1;}
  };
  
  class Dependency_Mock : public Dependency
  {
  public:
      MOCK_METHOD1(plusOne, int(int));
  };
  
  class SomeClass
  {
  public:
      void inject(std::unique_ptr&lt;Dependency&gt; dep) {dependency = std::move(dep);}
      int execute(int x) {return dependency-&gt;plusOne(x);}
  private:
      std::unique_ptr&lt;Dependency&gt; dependency;
  };
  
  
  TEST(SomeClassTest, executeTestWithMock)
  {
      SomeClass some;
      auto dep = std::make_unique&lt;Dependency_Mock&gt;();
      auto&amp; dep_ref = *(dep.get()); // This is kind of ugly.
      some.inject(std::move(dep));
  
      EXPECT_CALL( dep_ref , plusOne(_))
              .WillOnce(Return(5));
  
      EXPECT_EQ(some.execute(5), 5); // execute
  }
  </code></pre>