为什么这个简单的羽毛笔报价无法编译?

问题描述 投票:2回答:1

我正在尝试scala和quill(getquill.io)。下面这个最小的例子无法编译。这是为什么?它只定义了一个单独的类。我怀疑我需要以某种方式标记该类,以便能够解析它,但我不知道如何。因为我不需要标记案例类,他们应该只是工作,这是正确的吗?

package dbquerytest

import io.getquill._
/*in a real life you would rather pass execution context as
  a method or constructor argument, but we're just playing*/
import scala.concurrent.ExecutionContext.Implicits.global

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

case class Intake( id:Int, path:String, stage:Int) // , timestamp: Instant

// running/using junit test:https://alvinalexander.com/scala/how-to-use-junit-testing-with-scala
class MysqlLocalDbTest  {
  @Test
  def testIntake={
    val ctx = new MysqlAsyncContext(SnakeCase, "testdb")
    import ctx._
    val intakes = quote { query[Intake].map(_.id )}
    ctx.run(intakes).map(_.headOption)

    assertEquals(0,0)
  }
}

编译在io.getquill.quotation.Parsing失败。

scala quill
1个回答
4
投票

首先,我可以看到你在代码片段中使用了JUnit 5,但似乎存在一些问题。使用JUnit 5和Scala以及sbt:https://github.com/sbt/junit-interface/issues/75。替代方案包括使用JUnit 4或其中一个Scala特定的测试库,如ScalaTest或specs2(ScalaCheck也提到,尽管我通常只将它与ScalaTest或specs2结合使用)。

第二,我不知道你正在使用哪个构建工具,如果你有所有相关的依赖项,这可能是编译错误的原因。如果您正在使用sbt(https://www.scala-sbt.org/),我认为这是使用Scala进行开发时最常用的构建工具,可能的示例是如何查找您的示例(使用JUnit 4):

build.sbt:

import Dependencies._

ThisBuild / scalaVersion     := "2.12.8"
ThisBuild / version          := "0.1.0-SNAPSHOT"
ThisBuild / organization     := "com.example"
ThisBuild / organizationName := "example"

lazy val root = (project in file("."))
  .settings(
    name := "quilltesting",
    libraryDependencies ++= Seq(
      "mysql" % "mysql-connector-java" % "8.0.15",

      "io.getquill" %% "quill-jdbc" % "3.1.0",
      "io.getquill" %% "quill-async-mysql" % "3.1.0",

      // JUnit 4.
      "com.novocode" % "junit-interface" % "0.11" % Test
    )
  )

要从头开始生成快速项目以使用sbt进行测试,请在某处创建一个新文件夹,从命令行进入该路径并运行sbt new sbt/scala-seed.g8。然后进入该文件夹,然后运行sbt。之后,运行test

我已经将您的示例更改为使用JUnit 4,它似乎编译并运行:

package dbquerytest

import io.getquill._

/*in a real life you would rather pass execution context as
  a method or constructor argument, but we're just playing*/
import scala.concurrent.ExecutionContext.Implicits.global

import org.junit.Test
import junit.framework.TestCase
import org.junit.Assert._

case class Intake( id:Int, path:String, stage:Int)

// running/using junit test:https://alvinalexander.com/scala/how-to-use-junit-testing-with-scala
class MysqlLocalDbTest  {
  @Test
  def testIntake = {
    val ctx = new MysqlAsyncContext(SnakeCase, "testdb")
    import ctx._
    val intakes = quote { query[Intake].map(_.id )}
    ctx.run(intakes).map(_.headOption)

    assertEquals(0,0)
  }
}

如果你想尝试其他一些例子,还有https://scastie.scala-lang.org/QwOewNEiR3mFlKIM7v900A,与https://getquill.io/#quotation-introduction相关联。

© www.soinside.com 2019 - 2024. All rights reserved.