specs2 - A simple Scala Given/When/Then style specification failed -
i new spec2, , trying learn it. come following codes,
@runwith(classof[junitrunner]) class gwtstylespec extends specification { "a given-when-then example addition" ^ "given following number: ${1}" ^ number1 ^ "and second number: ${2}" ^ number2 ^ "then should get: ${3}" ^ result ^ end val number1: given[int] = (_: string).toint val number2: when[int, (int, int)] = (n1: int) => (s: string) => (n1, s.toint) val result: then[(int, int)] = (n: (int, int)) => (s: string) => ((n._1 + n._2) must_== s.toint) }
after run it, got java.lang.exception: not instantiate class com.me.scala.start.gwtstylespec: null , lots of exception stack trace below it.
what did wrong this?
if importing org.specs2.specification
there should def = ...
method defined:
@runwith(classof[junitrunner]) class gwtstylespec extends specification { def = "a given-when-then example addition" ^ "given following number: ${1}" ^ number1 ^ "and second number: ${2}" ^ number2 ^ "then should get: ${3}" ^ result ^ end lazy val number1: given[int] = (_: string).toint lazy val number2: when[int, (int, int)] = (n1: int) => (s: string) => (n1, s.toint) lazy val result: then[(int, int)] = (n: (int, int)) => (s: string) => ((n._1 + n._2) must_== s.toint) }
it possible val
s not being instantiated can try using lazy val
s.
note next specs2 version (1.15-snapshot) proposes another style of given/when/then specifications based on scala 2.10 features:
class givenwhentheninterpolatedspec extends specification givenwhenthen { def = sequential ^ s2""" given-when-then example calculator given following number: 1 $anumber , second number: 2 $anumber , third number: 6 $anumber when use operator: + $operator should get: 9 $result , should >: 0 $greaterthan multiplication given following number: 4 $anumber , second number: 5 $anumber , third number: 6 $anumber when use operator: * $operator should get: 120 $result , should >: 10 $greaterthan not should >: 150 $lowerthan """ val readint = groupas("\\d+") val readoperator = readas(".*: (.)$") val anumber: given[int] = readint , { s: string => s.toint } // when there many given[t, s] consecutive steps, possible follow them when[seq[t], s] val operator: when[seq[int], operation] = readoperator , { (numbers: seq[int]) => (s: string) => operation(numbers, s) } val result: then[operation] = readint andthen { (operation: operation) => (s: string) => operation.calculate must_== s.toint } val greaterthan: then[operation] = readint andthen { (operation: operation) => (s: string) => operation.calculate must be_>= (s.toint) } val lowerthan: then[operation] = readint andthen { (operation: operation) => (s: string) => operation.calculate must be_<= (s.toint) } case class operation(numbers: seq[int], operator: string) { def calculate: int = if (operator == "+") numbers.sum else numbers.product } }
Comments
Post a Comment