Specs2中类似小黄瓜的示例

时间:2019-01-29 08:37:06

标签: specs2

类似Gherkin的语法对于将示例放在规范的底部非常有用,但是不幸的是,从文档中可以看出Specs2不支持它。 (尽管它支持Tables,但我无法找到Tables和GWT的示例。用于GWT语法的解析器也无法解决此问题)

我们想出了以下代码,有什么

class ProcessManagerExample(startActivity:String) extends S2StringContextCreation
  with GWT with SpecificationStructure{



  def is =
    s2"""
         Given I started the process manager for my process ${g1}
         When a process is created at $startActivity $w1
         Then the external task is picked up by an external task processor and fails $t1
  """

  def g1 = step{

  }

  def w1 = step{

  }

  def t1 = {
    ok
  }
}

所以我们有以下问题:

  • 还有什么可以做得更好的方法,例如使用Tables吗?
  • 如何导入“更大”规范中的所有片段?

1 个答案:

答案 0 :(得分:0)

我们发现了一个利用Specs2 Fragments API的有趣解决方案,它使用ScenarioOutline类和ScenarioClass

import org.specs2.Specification
import org.specs2.concurrent.ExecutionEnv
import org.specs2.specification.create.S2StringContext
import org.specs2.specification.dsl.ActionDsl
import org.specs2.matcher.MustMatchers._
import org.specs2.specification.core.{Fragments, SpecStructure}

import scala.concurrent.Future

trait Service{
  def login:Future[User]
}

trait User{
  def submit(input:String):Future[String]
}


class MyScenarioOutline(input:String, expected:String, service:Service) extends S2StringContext with ActionDsl {

  implicit val executionEnv = ExecutionEnv.fromGlobalExecutionContext

  def scenarioOutline =
    s2"""
         Given that I am a logged user $g1
         When I submit the action with input $input $w1
         Then I should get a status of ${expected} $t1
         """

  var user:Future[User] = _
  var result:Future[String] = _

  def g1 = step{
    user = service.login
  }

  def w1 = step{
    result = user.flatMap{ u => u.submit(input)}
  }

  def t1 = {
    result must beEqualTo(expected).await
  }

}

class MyScenarioSpec extends Specification{

  val examples = List(
                      ("myInput1","myOutput2"),
                      ("myInput2", "myOutput2")
              )


  override def is: SpecStructure = {
    val importedFragments = examples.map{ case (input,output) => new VatCodeServiceAkkaImplGwt(input, output).scenarioOutline }
    val headerFragments = Fragments.apply(List(fragmentFactory.text("MyHeader"), fragmentFactory.break):_*)
    (List(headerFragments) ++ importedFragments).foldLeft(Fragments.empty){
      (previous,current) => previous.append(current)
    }
  }

}