1

我很难将我的scala测试类中的东西从specs“转移”到specs2。我有的最后一件事是问题doBefore{}和一些"test" in {}

"testing" should { doBefore{} 和一些人 "getting" in {} }给我这个错误

说明 资源路径 位置 类型

找不到 org.specs2.execute.AsResult[Unit] 类型的证据参数的隐式值

我假设“单元”是我项目中的类,但两者doBeforein {}没有返回任何东西,所以我不知道发生了什么。

doBefore的只是用随机值填充一些类,例如(这个在类中extends SpecificationWithJUnit with TestUtil with BeforeExample with AfterExample

"retrieving and counting users by criteria" should {

    var user1: User = null
    var user2: User = null
    var user3: User = null

    doBefore {
        val params = Map("login" -> "notExist", "roles" -> Seq(someRoles).asJava
        val params2 = Map("login" -> "notExistAnother", "roles" -> Seq(someRoles).asJava
        val params3 = Map("login" -> "notExistAnotherAnother", "roles" -> Seq(someRoles).asJava).asJava
        val users = Seq(params, params2, params3).map( { PopulateUser.insertUserParams(_).asInstanceOf[User] })
        user1 = users(0)
        user2 = users(1)
        user3 = users(2)
    }

我对 Scala 很陌生,但我在 specs2 中读到 doBefore 看起来不同,但老实说,我不知道我应该如何在我的代码中实现它。我正在读这个。所以有人知道我应该如何在我的代码中实现它以及导致它的原因(我的意思是 beetwen specs 和 specs2 的差异很大,但不知何故我的测试(除了 doBefore)很少出现相同的错误)

4

2 回答 2

1

您的测试没有测试任何内容。方法中的最后一个表达式是方法的返回值,它必须是specs2可以转换为Result的东西。您返回的最后一个值是 do before 的结果,它是Unit,它不能转换为 test**Result**。这就是给出的错误的来源。

could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[Unit]

doBefore你使用它很好,但之后应该有某种测试。

有关更多信息,请查看http://etorreborre.github.io/specs2/guide/org.specs2.guide.Structure.html#Structure有一个特殊部分描述了如何在Specs2 单元和验收测试中使用之前之后。

一般来说,您可以从切换到验收测试风格中获得很多收益。

于 2014-04-25T15:56:48.790 回答
0

specs2 中的上下文与 specs 中的管理方式不同。如果你想在一组示例之前执行一个动作,你需要创建一个Step

"retrieving and counting users by criteria" should {

var user1: User = null
var user2: User = null
var user3: User = null

step {
  val params = Map("login" -> "notExist", "roles" -> Seq(someRoles).asJava
  val params2 = Map("login" -> "notExistAnother", "roles" -> Seq(someRoles).asJava
  val params3 = Map("login" -> "notExistAnotherAnother", "roles" -> Seq(someRoles).asJava).asJava
    val users = Seq(params, params2, params3).map( { PopulateUser.insertUserParams(_).asInstanceOf[User] })
  user1 = users(0)
  user2 = users(1)
  user3 = users(2)
}

"first example" >> ...

如果您想在每个示例之前执行一些代码,您可以混合BeforeExample特征并实现该before方法。

最后,如果您想避免使用变量并将一些数据传递给每个示例,您可以使用FixtureExample[T]trait:

class MySpec extends Specification with FixtureExample[Data] {
  def fixture[R : AsResult](f: Data => R) = {
    val data: Data = ??? // prepare data
    AsResult(f(data))
  }

  "a group of example" >> {
    "example1" >> { data: Data =>
      ok
    }
  }
}
于 2014-04-25T21:20:39.653 回答