2015-05-26 52 views
2
abstract trait MyApi { 

    def getResult()(implicit ec: ExecutionContext): Future[String] 

} 

以下的方法不工作:模擬不帶參數但隱含參數

val m = mock[MyApi] 
(m.getResult _).expects() returning "..." 

它失敗:

java.lang.ClassCastException: org.scalamock.MockFunction1 cannot be cast to org.scalamock.MockFunction0 

注:http://scalamock.org/user-guide/advanced_topics/給出的例子是隻有當方法至少有一個參數時纔有用。所以我們不能使用如下解決方案:mocking methods which use ClassTag in scala using scalamock

回答

4

我想你沒有看正確的例子。請看例子4隱含參數:

class Codec() 

trait Memcached { 
    def get(key: String)(implicit codec: Codec): Option[Int] 
} 

val memcachedMock = mock[Memcached] 

implicit val codec = new Codec 
(memcachedMock.get(_ : String)(_ : Codec)).expects("some_key", *).returning(Some(123)) 

在你的情況,當然,非隱PARAMS是空的,所以你想:

(m.getResult()(_: ExecutionContext)).expects(*) returning "..." 
+0

解決的辦法是:(m.getResult() (_:ExecutionContext))。期望(*)返回「...」,如果你更新你的答案,我會接受它。 – douglaz

+0

@douglaz - done-Ed –