Specs2:如何测试具有多个注入依赖项的类? [英] Specs2: how to test a class with more than one injected dependency?

查看:22
本文介绍了Specs2:如何测试具有多个注入依赖项的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Play 2.4 应用,对服务类使用依赖注入.

Play 2.4 app, using dependency injection for service classes.

我发现当被测试的服务类具有多个注入依赖项时,Specs2 会卡住.它失败了找不到类的构造函数......"

I found that Specs2 chokes when a service class being tested has more than one injected dependency. It fails with "Can't find a constructor for class ..."

$ test-only services.ReportServiceSpec
[error] Can't find a constructor for class services.ReportService
[error] Error: Total 1, Failed 0, Errors 1, Passed 0
[error] Error during tests:
[error]         services.ReportServiceSpec
[error] (test:testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed Dec 8, 2015 5:24:34 PM

生产代码,精简到最低限度以重现此问题:

Production code, stripped to bare minimum to reproduce this problem:

package services

import javax.inject.Inject

class ReportService @Inject()(userService: UserService, supportService: SupportService) {  
   // ...  
}

class UserService {  
   // ...  
}

class SupportService {  
   // ...  
}

测试代码:

package services

import javax.inject.Inject

import org.specs2.mutable.Specification

class ReportServiceSpec @Inject()(service: ReportService) extends Specification {

  "ReportService" should {
    "Work" in {
      1 mustEqual 1
    }
  }

}

如果我从 ReportService 中删除 UserServiceSupportService 依赖项,则测试有效.但显然,依赖关系在生产代码中是有原因的.问题是,我该如何进行此测试?

If I remove either UserService or SupportService dependency from ReportService, the test works. But obviously the dependencies are in the production code for a reason. Question is, how do I make this test work?

编辑:当尝试在 IntelliJ IDEA 中运行测试时,同样的事情失败了,但有不同的消息:测试框架意外退出"、这看起来像规范 2 异常......";请参阅带有堆栈跟踪的完整输出.我按照输出中的说明打开了一个 Specs2 issue,但我不知道问题是否存在在 Play 或 Specs2 或其他地方.

Edit: When trying to run the test inside IntelliJ IDEA, the same thing fails, but with different messages: "Test framework quit unexpectedly", "This looks like a specs2 exception..."; see full output with stacktrace. I opened a Specs2 issue as instructed in the output, though I have no idea if the problem is in Play or Specs2 or somewhere else.

我的库依赖项如下.(我尝试指定 Specs2 版本 明确,但这没有帮助.看起来我需要 specs2 % Test 原样,以便像 WithApplication 这样的 Play 测试类工作.)

My library dependencies below. (I tried specifying Specs2 version explicitly, but that didn't help. Looks like I need specs2 % Test as is, for Play's test classes like WithApplication to work.)

resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
libraryDependencies ++= Seq(
  specs2 % Test,
  jdbc,
  evolutions,
  filters,
  "com.typesafe.play" %% "anorm" % "2.4.0",
  "org.postgresql" % "postgresql" % "9.4-1205-jdbc42"
)

推荐答案

specs2 中对依赖注入的支持有限,主要用于执行环境或命令行参数.

There is limited support for dependency injection in specs2, mostly for execution environments or command-line arguments.

没有什么可以阻止您只使用 lazy val 和您最喜欢的注入框架:

There is nothing preventing you from just using a lazy val and your favourite injection framework:

class MySpec extends Specification with Inject {
  lazy val reportService = inject[ReportService]

  ...
}

使用 Play and Guice,您可以拥有这样的测试助手:

With Play and Guice, you could have a test helper such as this:

import play.api.inject.guice.GuiceApplicationBuilder
import scala.reflect.ClassTag    

trait Inject {
  lazy val injector = (new GuiceApplicationBuilder).injector()

  def inject[T : ClassTag]: T = injector.instanceOf[T]
}

这篇关于Specs2:如何测试具有多个注入依赖项的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆