[英]Mocking Scala Trait using Scala, ScalaTest, and Mocktio
無論出於何種原因,Mocktio都不會模擬特征中具有的方法,它將調用實際方法。 這是我的測試:
"displays the index page" in {
val mockAuth = mock[AuthMethods]
when(mockAuth.isAllowed(-1, "", "")).thenReturn(true)
val controller = new TestController()
val result = controller.index().apply(FakeRequest())
val bodyText = contentAsString(result)
bodyText must include ("Name")
}
這是特征和對象:
trait AuthMethods {
def isAllowed(userID:Long, method:String, controller:String) : Boolean = {
//do stuff..
}
object Authorized extends AuthMethods with ActionBuilder [Request] {
def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) = {
if(isAllowed(userID, method, controller) {
//do some more stuff..
}
關於為什么調用實際方法與模擬方法相比有何想法? 我正在使用Scala 2.10.4。 任何幫助,將不勝感激。
我忘了提一下,“授權”是一個動作組合,使用方式如下:
def index = Authorized {
Ok(html.Stations.index(Stations.retrieveAllStations))
}
您已經創建了模擬實現mockAuth
但尚未對其執行任何操作。 創建模擬實現不會神奇地導致它替換某些其他對象。 看來您想創建Authorized
對象的模型並安排您的TestController使用它。 您可能必須在某個地方打破依賴關系。
(已更新)由於這是在Play框架的上下文中,因此您可能會發現此博客文章很有幫助。 它描述了與您類似的情況。 看來您將不得不更改對Authorized
對象的引用方式,以提供模擬實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.