簡體   English   中英

規范和規范2:如何在規范2中實現doBefore {}?

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

我在將scala測試類中的內容從“規范”轉移到specs2時遇到了麻煩。 我的最后一件事是doBefore{}doBefore{}一些"test" in {}

我的"testing" should { doBefore{} "testing" should { doBefore{} 一些 "getting" in {} }給我這個錯誤

說明資源路徑位置類型

找不到類型為org.specs2.execute.AsResult [Unit]的證據參數的隱式值

我假設“ 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之前)卻避免了相同的錯誤)

您的測試不會測試。 方法中的最后一個表達式是該方法的返回值,該值必須是specs2可以將其轉換為Result的值 您返回的最后一個值是do的結果,之前的結果是Unit ,無法將其轉換為測試** 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單元和驗收測試中使用“ 之前之后”

通常,從切換到驗收測試樣式可以獲得很多收益。

spec2中的上下文與規范中的管理方式不同。 如果要在一組示例之前執行操作,則需要創建一個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]特性:

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