【问题标题】:Testing injected controllers in Play for Scala在 Play for Scala 中测试注入的控制器
【发布时间】:2016-12-23 04:07:12
【问题描述】:

我正在尝试按照 Play 文档中的 this example 来测试带有注入对象的控制器。我复制了示例,但在尝试调用网页时出现错误:

未绑定 test.Component 的实现。

这个错误似乎是正确的,因为我没有调用binding 方法,但是如何解决这个问题?

这是我的代码:

package test

import play.api.mvc._
import javax.inject.Inject
import play.api.{ Environment, Configuration }
import play.api.inject.Module

trait Component {
  def hello: String
}

class DefaultComponent extends Component {
  def hello = "default"
}

class MockComponent extends Component {
  def hello = "mock"
}


class ComponentModule extends Module {
  def bindings(env: Environment, conf: Configuration) = Seq(
    bind[Component].to[DefaultComponent]
  )
}

class Application @Inject() (component: Component) extends Controller {
  def index() = Action {
    Ok(component.hello)
  }
}

【问题讨论】:

  • 你是如何运行这段代码的?你如何确保ComponentModule 被游戏识别?
  • 我在路由 GET /testapp test.Application.index 中有一个条目。我不知道如何让 ComponentModule 被 Play 识别,这是 Play 示例中缺少的部分,请参阅问题中的链接。

标签: scala playframework scalatest playframework-2.5


【解决方案1】:

添加到 application.conf

play {
    modules {
        enabled += test.ComponentModule 
    }
}

【讨论】:

    猜你喜欢
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多