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

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

问题描述

播放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

生产代码这个问题:

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 中删除​​ UserService SupportService ,测试工作。但显然,依赖关系在生产代码中是一个原因。 问题是,如何使此测试工作?

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,同样的事情失败,但有不同的消息:测试框架意外退出,这看起来像一个specs2异常...;请参阅使用堆栈跟踪完整输出。我按照输出中的说明打开了Specs2 问题,尽管我不知道是否有问题在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 ,因为Play的测试类,如 WithApplication 可以正常工作。)

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和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天全站免登陆