如何在单元测试中将配置对象传递给控制器

时间:2019-06-17 20:53:16

标签: scala playframework configuration scalatest

我有一个控制器,如下所示:

class MyController @Inject()
(
  cc : ControllerComponents,
) extends AbstractController(cc) with I18Support(

  def controllerMethod() = Action{
   ... //some impl.
  }
)

我正在Scalatest中测试控制器,如下所示:

"My Controller" when {

  "a user hits this controller method" should {

  val controller = new MyController( cc = stubMessageControllerComponents )

  "be a 200 OK" in {
    whenReady(controller.mycontrollerMethod().apply(FakeRequest("GET", "/"))) {
    // some test

   }

我的问题是,现在我已经更改了控制器类,以如下方式注入配置对象

class MyController @Inject()
(
  config : Configuration,
  cc : ControllerComponents,
) extends AbstractController(cc) with I18Support(

  def controllerMethod() = Action{
   ... //some impl.
  }
)

我现在在测试中收到编译错误,因为我没有传递Configuration对象。我该怎么办?

"My Controller" when {

  "a user hits this controller method" should {

  val controller = new MyController(
    // <- how can I pass a configuration object here 
    cc = stubMessageControllerComponents 
  )

  "be a 200 OK" in {
    whenReady(controller.mycontrollerMethod().apply(FakeRequest("GET", "/"))) {
    // some test

   }

1 个答案:

答案 0 :(得分:1)

尝试

new MyController(
  config = Configuration(ConfigFactory.load()) 
  cc = stubMessageControllerComponents 
)