Огурецоподобные примеры в Specs2
Подобный корнишону синтаксис очень полезен для размещения примеров в нижней части спецификации, и, к сожалению, из документации видно, что 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
}
}
Итак, у нас есть следующие вопросы:
- Есть ли что-нибудь лучшее, что можно сделать, возможно, с помощью таблиц?
- Как импортировать все фрагменты в нашей "большей" спецификации?
1 ответ
Мы нашли интересное решение, использующее API Specs2 Fragments, используя 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)
}
}
}