1

我想对嵌套在对象下的方法的返回值执行单元测试。例子:

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;
        }
    }

}
4

2 回答 2

1

您可以将地图作为 a 传递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
      }
    }
  }
}
于 2013-04-03T14:09:56.163 回答
1

specs2 中有一个foreach方法(或者foreachWhen如果您更喜欢使用 a 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) }
}
于 2013-04-04T13:39:56.130 回答