【发布时间】:2014-07-25 20:34:40
【问题描述】:
无论出于何种原因,Mocktio 都不会模拟我在 trait 中拥有的方法,它会调用实际的方法。这是我的测试:
"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。任何帮助,将不胜感激。
我忘了说,Authorized 是一个动作组合,下面是它的使用方式:
def index = Authorized {
Ok(html.Stations.index(Stations.retrieveAllStations))
}
【问题讨论】:
-
您的
TestController是如何实现其AuthMethods的?显示该代码可能有助于我们分析。 -
@DonRoby 我添加了它是如何使用授权对象的。