【问题标题】:Scala / specs2 : could not find implicit value for evidence parameter of type AsExecution[ExecutionEnv => MatchResult[Future[AuthenticationResult]]]Scala / specs2:找不到 AsExecution[ExecutionEnv => MatchResult[Future[AuthenticationResult]]] 类型的证据参数的隐式值
【发布时间】:2020-09-14 04:46:45
【问题描述】:

我正在尝试将 Scala/Play 应用程序升级到 Play 2.7Scala 2.12.11

在升级 Play 和 Scala 之前,我进行了以下测试。

升级后出现如下编译错误:

Error: could not find implicit value for evidence parameter of type org.specs2.specification.core.AsExecution[org.specs2.concurrent.ExecutionEnv => org.specs2.matcher.MatchResult[scala.concurrent.Future[services.AuthenticationResult]]]

import org.specs2.concurrent.ExecutionEnv
import org.specs2.mutable.Specification

import scala.concurrent.duration._

class AuthenticationServiceSpec extends Specification {

  "The AuthenticationService" should {

    val service: AuthenticationService = new MyAuthenticationService

    "correctly authenticate Me" in { implicit ee: ExecutionEnv =>
      service.authenticateUser("myname", "mypassowrd") must beEqualTo (AuthenticationSuccessful).await(1, 200.millis)
    }

  }

}

为了尝试解决编译错误,我在类构造函数中添加了一个隐式参数(顺便说一句,之前它是如何工作的,没有隐式参数?):

import org.specs2.concurrent.ExecutionEnv
import org.specs2.mutable.Specification

import scala.concurrent.duration._
import scala.concurrent.Future

import org.specs2.specification.core.AsExecution
import org.specs2.matcher.MatchResult    

class AuthenticationServiceSpec(implicit ee: AsExecution[ExecutionEnv => MatchResult[Future[AuthenticationResult]]]) extends Specification {

  "The AuthenticationService" should {

    val service: AuthenticationService = new MyAuthenticationService

    "correctly authenticate Me" in { implicit ee: ExecutionEnv =>
      service.authenticateUser("myname", "mypassowrd") must beEqualTo (AuthenticationSuccessful).await(1, 200.millis)
    }

  }

}

但是在运行时,当我运行测试时,我得到了错误:

Can't find a suitable constructor with 0 or 1 parameter for class org.specs2.specification.core.AsExecution

基于AsExecution的构造函数,有道理……

那我该如何解决这个问题呢?

【问题讨论】:

    标签: scala playframework implicit specs2


    【解决方案1】:

    这应该可以工作

    class AuthenticationServiceSpec(implicit ee: ExecutionEnv) extends Specification {
    
      "The AuthenticationService" should {
    
        val service: AuthenticationService = new MyAuthenticationService
    
        "correctly authenticate Me" in {
          service.authenticateUser("myname", "mypassword") must beEqualTo (AuthenticationSuccessful).await(1, 200.millis)
        }
    
      }
    
    }
    

    ee: ExecutionEnv 将在构建时直接注入规范中。

    这就是消息Can't find a suitable constructor with 0 or 1 parameter for class ... 的含义。 specs2 尝试递归地为规范构建参数,如果它们有一个带有 0 或 1 个参数的构造函数。 ExecutionEnv 就是这样一个论点,specs2 能够自行构建。

    【讨论】:

    • 谢谢 :) 我认为方法参数应该是隐式的。原来的AsExecution[ExecutionEnv => MatchResult[Future[AuthenticationResult]]] 试图做什么? AsExecution 有两个隐式方法(resultAsExecutionfutureAsExecution),我想知道它们有什么用处。如果没有隐式参数,原始代码是如何工作的?
    • 在示例级别使用implicit ee: ExecutionEnv => 的原始代码有效,因为specs2 从ExecutionEnv => R 其中R : AsResult 转换为Execution(因为最终规范只有Fragment = (Description, Execution) 的流. 我最终将ExecutionEnv 的提供作为规范级别的一种依赖注入来减少每个示例的重复(以及在必要时注入其他数据的方式)。是的ee参数需要是隐式的,我固定了答案。
    猜你喜欢
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2013-01-25
    • 2017-04-26
    • 1970-01-01
    相关资源
    最近更新 更多