title: Using a mocked object as an implicit in scala using scalamock
body: <p>I am using a trait definition with various concrete derivatives and implicit to inject dependancies into objects and also to mock out parts of the system when unit testing.  The problem is that when a mocked version of a type is used as an implicit declaration, it is not matched by scala in the consuming object. </p>
  
  <p>Here is a simplified version of my setup.  Is there a way to make <em>Test1</em> work using a mock.  <em>Test2</em> works fine but it hard to maintain and requires too much setup.</p>
  
  <p>A model:</p>
  
  <pre><code>case class User (first: String, last: String, enabled: Boolean)
  </code></pre>
  
  <p>Component Definition:</p>
  
  <pre><code>trait DataProviderComponent {
    def find[T](id: Int): Try[T]
    def update[T](data: T): Try[T]
  }
  </code></pre>
  
  <p>One of the concrete component implementations:</p>
  
  <pre><code>class DbProvider extends DataProviderComponent {
    override def find[T](id: Int): Try[T] = { ... }
    override def update[T](data: T): Try[T] = { ... }
  }
  </code></pre>
  
  <p>Implicit usage of component impl somewhere in system:</p>
  
  <pre><code>implicit val provider = new DbProvider()
  
  class UserRepsitory(implicit provider: DataProviderComponent) {
    def userEnabled(id: Int): Boolean = {
      val user = provider.find[User](id)
      user.isSuccess &amp;&amp; user.get.enabled
    }
  }
  </code></pre>
  
  <p>Unit Test1, trying to mock out provider in order to isolate repository test.  This does not work, the repository class does not match the implicit even though it is based on DataProviderComponent:</p>
  
  <pre><code>class UserRepository$Test1 extends FunSuite with Matchers with MockFactory {
    test("invalid data request should return false") {
      implicit val  mockProvider = mock[DataProviderComponent]
      (mockProvider.find[User] _).expects(13).returns(Failure[User](new Exception("Failed!")))
  
      val repo = new UserRepsitory()
      repo.userEnabled(13) should be (false)
    }
  }
  </code></pre>
  
  <p>This version does work but is hard to maintain and requires more code:</p>
  
  <pre><code>class UserRepository$Test2 extends FunSuite with Matchers with MockFactory {
    test("invalid data request should return false") {
      class FakeProvider extends DataProviderComponent {
        override def find[T](id: Int): Try[T] = ???
        override def update[T](data: T): Try[T] = Failure[T](new Exception("Failed!"))
      }
  
      implicit val provider = new FakeProvider()
      val repo = new UserRepsitory()
      repo.userEnabled(13) should be (false)
    }
  }
  </code></pre>
  
  <p>Is there a way to use a mocked type as an injected implicit - or is there another scala-thonic a pattern I should use to solve this problem?</p>