在同一特征之前和之后

时间:2015-10-01 08:55:54

标签: scala specs2

我在scala中有下一个代码测试,我希望在每个测试之前和之后运行方法之前和之后,但我有一个错误

这是我的测试

import java.util.{Calendar, UUID}
import com.smartchat.helpers.BeforeAllAfterAll
import com.smartchat.models.SmartChat.{ComplaintReasonType,
ComplaintContentType}
import org.junit.runner.RunWith
import org.specs2.execute.{Result, AsResult}
import org.specs2.mutable.{After, Before}
import org.specs2.runner.JUnitRunner
import scala.concurrent.duration._
import scala.concurrent.{Future, Await}


  class ComplaintModelTest extends DatabaseSpec{

  class TestingEnvironment extends ComplaintComponentImpl 
  with StorageComponentImpl

  val environment = new TestingEnvironment

  "Complaint model" should {


    "test get complaints by reason" in new BeforeAfterEach {
      ok
    }

    "test get complaints by type" in new BeforeAfterEach {
      ok
    }
  }

}

trait BeforeAfterEach extends Before with After{

  override def before = {
    println("++++++++++++++")
  }

  override def after = {
    println("--------------")
  }

}

这是错误的输出

trait BeforeAfterEach inherits conflicting members:
[error]   method apply in trait Before of type [T](a: => T)(implicit evidence$3: org.specs2.execute.AsResult[T])org.specs2.execute.Result  and
[error]   method apply in trait After of type [T](a: => T)(implicit evidence$1: org.specs2.execute.AsResult[T])org.specs2.execute.Result
[error] (Note: this can be resolved by declaring an override in trait BeforeAfterEach.)
[error] trait BeforeAfterEach extends Before with After{
[error]       ^
[error] one error found
[error] (test:compileIncremental) Compilation failed

我不知道如何修复它。 我希望在每个测试示例之前和之后运行方法

2 个答案:

答案 0 :(得分:1)

如果您想在before中定义afterBeforeAfterEach方法,那么您应该继承org.specs2.mutable.BeforeAfter

答案 1 :(得分:1)

实现此目的的推荐方法是使用现有的org.specs2.specification.BeforeAfterEach特征:

class MySpec extends Specification with BeforeAfterEach {
  def before = println("before")
  def after = println("after")

  "test 1" >> ok
  "test 2" >> ok
}