【问题标题】:Gherkin-like examples in Specs2Specs2 中类似 Gherkin 的示例
【发布时间】:2019-06-22 07:58:17
【问题描述】:

类似 Gherkin 的语法对于将示例放在规范底部非常有用,不幸的是,从文档中可以看出 Specs2 不支持它。 (虽然它支持表格,但我找不到表格和 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
  }
}

所以我们有以下问题:

  • 还有什么可以做的更好的吗,也许是使用表格?
  • 如何导入我们“更大”规范中的所有片段?

【问题讨论】:

    标签: specs2


    【解决方案1】:

    我们发现了一个利用 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)
        }
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 2012-11-06
      相关资源
      最近更新 更多