在 Play for Scala 中测试注入控制器
Testing injected controllers in Play for Scala
我正在尝试按照 Play 文档中的 this example 来测试带有注入对象的控制器。我复制了示例,但在尝试调用网页时出现错误:
No implementation for test.Component was bound.
错误似乎是正确的,因为我没有调用 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)
}
}
添加到application.conf
play {
modules {
enabled += test.ComponentModule
}
}
我正在尝试按照 Play 文档中的 this example 来测试带有注入对象的控制器。我复制了示例,但在尝试调用网页时出现错误:
No implementation for test.Component was bound.
错误似乎是正确的,因为我没有调用 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)
}
}
添加到application.conf
play {
modules {
enabled += test.ComponentModule
}
}