简体   繁体   English

规范和规范2:如何在规范2中实现doBefore {}?

[英]specs and specs2: how to implement doBefore{} in specs2?

I'm having hard time with "transfering" things in my scala test classes from specs to specs2. 我在将scala测试类中的内容从“规范”转移到specs2时遇到了麻烦。 Last thing I have is issue with doBefore{} and some "test" in {} 我的最后一件事是doBefore{}doBefore{}一些"test" in {}

My "testing" should { doBefore{} and some "getting" in {} } give me this error 我的"testing" should { doBefore{} "testing" should { doBefore{} 一些 "getting" in {} }给我这个错误

Description Resource Path Location Type 说明资源路径位置类型

could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[Unit] 找不到类型为org.specs2.execute.AsResult [Unit]的证据参数的隐式值

I assume that "Unit" is class in my project, but both doBefore and in {} don't return anything so I don't know whats goin on. 我假设“ Unit”在我的项目中是类,但是doBeforein {}都不返回任何内容,所以我不知道发生了什么。

My doBefore 's just fill some classes with random values for eg (this one is in class that extends SpecificationWithJUnit with TestUtil with BeforeExample with AfterExample 我的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)
    }

I'm pretty new to Scala, but Ive read that in specs2 doBefore looks diffrent but to be honest I don't know how should I implement this in my code. 我对Scala还是很陌生,但是我已经在specs2中读到了,doBefore看起来有所不同,但是老实说我不知道​​该如何在我的代码中实现它。 I was reading this . 我在读这篇 So someone know how should I implement this in my code and whats causing that (I mean diffrence beetwen specs and specs2 is huge, but somehow few my test (beside doBefore) are sedning the same error) 因此有人知道我应该如何在我的代码中实现它,以及导致该问题的原因(我的意思是beetwen specs和specs2差异很大,但是某种程度上,我的测试(在doBefore之前)却避免了相同的错误)

Your test tests nothing. 您的测试不会测试。 The last expression in your method is the return value of the method, which must be something that specs2 can convert to a Result . 方法中的最后一个表达式是该方法的返回值,该值必须是specs2可以将其转换为Result的值 The last value you return is the result of do before which is Unit , which can't be converted to a test**Result**. 您返回的最后一个值是do的结果,之前的结果是Unit ,无法将其转换为测试** Result **。 That is the source of the error given. 这就是给出的错误的根源。

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

doBefore as you use it is fine but afterwards there should be some kind of test. doBefore,你使用它是好的,但后来应该是一种考验。

For more information look at http://etorreborre.github.io/specs2/guide/org.specs2.guide.Structure.html#Structure there is a special section desccribing how to use Before and After with Specs2 Unit and Acceptance tests. 有关更多信息,请参见http://etorreborre.github.io/specs2/guide/org.specs2.guide.Structure.html#Structure,其中有一个特殊的部分描述了如何 Specs2单元和验收测试中使用“ 之前之后”

In general you can get much gain from switching to Acceptance Test style. 通常,从切换到验收测试样式可以获得很多收益。

The contexts in specs2 are managed differently than in specs. spec2中的上下文与规范中的管理方式不同。 If you want to execute an action before a group of examples you need to create a Step : 如果要在一组示例之前执行操作,则需要创建一个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" >> ...

If you want to execute some code before each example you mix-in the BeforeExample trait and implement the before method. 如果要在每个示例之前执行一些代码,请混合使用BeforeExample特征并实现before方法。

Finally if you want to avoid using variables and pass some data to each example you can use the FixtureExample[T] trait: 最后,如果您想避免使用变量并将一些数据传递给每个示例,则可以使用FixtureExample[T]特性:

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
    }
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM