我想对嵌套在对象下的方法的返回值执行单元测试。例如:
package code.learn
import org.specs2.mutable._;
import com.learning.run.CMMDC;
class testing extends Specification {
val t1 = Map(1 -> 6, 7 -> 12, 9 -> 13);
"testing the results" in {
foreach(t1) {
case (key, value) =>
CMMDC.compute(key, value) must_== value;
}
}
}
答案 0 :(得分:1)
您可以将地图作为Context
传递,这是一种定义灯具的方式。例如:
class testing extends Specification {
var results: Map[Int, Int] = _
val resultsMapOne = beforeContext(results = Map(1 -> 3, 5 -> 7))
"sample method" definedAs resultsOne should {
"with resultsMapOne" in {
results foreach {
case (key, value) => test.sampleMethod(key) must_== value
}
}
}
}
答案 1 :(得分:1)
在specs2中有a foreach
method(或foreachWhen
,如果您更喜欢使用PartialFunction
),它会测试给定示例的多个值:
"testing the results" in {
foreach(t1) { kv => test.sampleMethod(kv._1) must_== someList(kv._2) }
}
// or
"testing the results" in {
foreachWhen(t1) { case (k, v) => test.sampleMethod(k) must_== someList(v) }
}