简体   繁体   English

Scala-播放-如何在Test中模拟导入play.api.libs.concurrent.CustomExecutionContext并将其用作隐式参数?

[英]Scala - Play - How to mock import play.api.libs.concurrent.CustomExecutionContext in Test and use it as an implicit param?

I have a test which is testing a class that expects an implicit CustomExecutionContext: 我有一个测试正在测试期望隐式CustomExecutionContext的类:

@Singleton
class MyRepo @Inject()
(appConfigService: AppConfigService)
(implicit ec: RepositoryDispatcherContext)

Now I need to test this class and inject a mock dispatcher context during test. 现在,我需要测试该类,并在测试过程中注入模拟调度程序上下文。 Initially I was thinking of using the standard global execution context that ships out of the box. 最初,我考虑使用现成的标准全局执行上下文。

implicit executionContext = scala.concurrent.ExecutionContext.Implicits.global

But the test fails at it expects another type of instance: 但是由于期望其他类型的实例而导致测试失败:

could not find implicit value for parameter ec: common.executor.RepositoryDispatcherContext 找不到参数ec的隐式值:common.executor.RepositoryDispatcherContext

This is mine Custom execution context: 这是我的自定义执行上下文:

import javax.inject.{Inject}
import akka.actor.ActorSystem
import play.api.libs.concurrent.CustomExecutionContext

class RepositoryDispatcherContext @Inject()(actorSystem: ActorSystem) extends CustomExecutionContext(actorSystem, "repository.dispatcher")

Was wondering how to inject a mock instance of my custom execution context to be used as an implicit param in my Test class? 想知道如何将自定义执行上下文的模拟实例用作Test类中的隐式参数吗?

You can create a sub class of your custom dispatcher and override the necessary methods: 您可以创建自定义调度程序的子类,并覆盖必要的方法:

import org.specs2.mutable.Specification

import akka.actor.ActorSystem
import scala.concurrent.ExecutionContext

class MySomethingSpec extends Specification with Mockito {

  "MySomething" should {    

    "mock repository dispatcher itself" in {
      class MyMockedRepositoryDispatcher(executionContext: ExecutionContext) extends RepositoryDispatcherContext(ActorSystem()) {
        override def execute(command: Runnable) = executionContext.execute(command)
        override def reportFailure(cause: Throwable) = executionContext.reportFailure(cause)
      }

      val executionContext: ExecutionContext = ??? // whatever you need
      val repositoryDispatcher: RepositoryDispatcherContext = new MyMockedRepositoryDispatcher(executionContext)

      // do what you need
      // assertions
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用scala.concurrent.Promise替换play.api.libs.concurrent.Promise时出错 - Error replacing play.api.libs.concurrent.Promise with scala.concurrent.Promise 类型不匹配; 发现:scala.concurrent.Future [play.api.libs.ws.Response]必需:play.api.libs.ws.Response - type mismatch; found : scala.concurrent.Future[play.api.libs.ws.Response] required: play.api.libs.ws.Response 没有 play.api.libs.json.Format 的实例可用于 scala.Char 中的隐式 Z31A1ZEFD140BE4BEF25D81 - No instance of play.api.libs.json.Format is available for scala.Char in the implicit scope Scala / Play / REPL:配置错误(play.api.libs.Crypto) - Scala/Play/REPL: Configuration error (play.api.libs.Crypto) Scala和Play:如何使用我的隐式对象? - Scala and Play: how can I use my implicit object? Play Framework SBT导入play.api.libs.streams - Play Framework SBT import play.api.libs.streams No instance of play.api.libs.json.Format is available for scala.Option[java.sql.Timestamp] in the implicit scope - No instance of play.api.libs.json.Format is available for scala.Option[java.sql.Timestamp] in the implicit scope 向scala SBT项目添加依赖项(导入play.api.libs.json.Json) - add dependency to scala SBT project (import play.api.libs.json.Json) 无法将play.api.test._导入Scala REPL - Can't import play.api.test._ into scala REPL 如何在Scala Play框架中模拟外部WS API调用 - how to mock external WS API calls in Scala Play framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM