我试图用不同的模拟插件代替真实插件进行测试。它适用于一个模拟,但是当我有两个时,它总是使用第一个。
class UserSpec extends Specification {
"User" should {
val fakeAppA = new FakeApplication(
additionalPlugins = Seq(
"FakeServiceA"
)
)
"have FakeServiceA " in running(fakeAppA) {
UserController.doit() === "FakeServiceA"
}
val fakeAppB = new FakeApplication(
additionalPlugins = Seq(
"FakeServiceB"
)
)
"have FakeServiceB" in running(fakeAppB) {
// doesnt work gets FakeServiceA
UserController.doit() === "FakeServiceB"
}
}
}
object UserController extends Controller {
val service = Play.application.plugin[Service]
.getOrElse(throw new RuntimeException("Service not loaded"))
def doit() = service.serviceIt()
}
class Service(app: Application) extends Plugin {
def serviceIt(): String = "Service"
}
class FakeServiceA(app: Application) extends Service(app) {
override def serviceIt(): String = "FakeServiceA"
}
class FakeServiceB(app: Application) extends Service(app) {
override def serviceIt(): String = "FakeServiceB"
}
答案 0 :(得分:1)
不为每个测试创建控制器,因为它是一个scala对象。因此,您应该通过def而不是val来引用插件。
object UserController extends Controller {
- val service = Play.application.plugin[Service]
+ def service = Play.application.plugin[Service]
.getOrElse(throw new RuntimeException("Service not loaded"))