我正在使用最新的FlatSpec库来测试我的代码。 下面是我为了测试代码而编写的函数:
def compareToHello (a:String) = {
a match {case "Hello" => println ("Hello")
case _ => println("error")}
}
对于测试部分:
import org.scalatest.{ FlatSpec, GivenWhenThen, Matchers }
class AestA extends FlatSpec with GivenWhenThen with Matchers {
"Implemented function" should "compare input string to hello"in {
val test=compareToHello("Hello)
assert
}
}
由于我的功能输出显示在控制台中,因此我遇到了关于断言内容的问题。 我正在学习scala,这就是我提出这类问题的原因 非常感谢
答案 0 :(得分:0)
public void fillGaps() {
tempGaps = new int[difference];
finalTemps = new int[difference];
for (int i = 0; i < difference; i++) { //navigate through every temperature
tempGaps[i] = temp[0] + i; //assign the current temperature
Integer frequency = map.get(new Integer(tempGaps[i])); //fetch the frequency
finalTemps[i] = frequency == null ? 0 : frequency; //assign 0 if null, otherwise frequency if not null
}
}
是一个副作用:您无法推断println
的输出。
提取
compareToHello
你可以做的是通过返回一个字符串来使这个函数变得纯净:
scala> def f: Unit = println("Hello")
f: Unit
scala> f
Hello
scala> f == "Hello"
<console>:9: warning: comparing values of types Unit and String using `==' will always yield false
f == "Hello"
^
Hello
res7: Boolean = false
scala> f == ()
<console>:9: warning: comparing values of types Unit and Unit using `==' will always yield true
f == ()
^
warning: there was one deprecation warning; re-run with -deprecation for details
Hello
res8: Boolean = true
并断言如下:
def compareToHello (a:String) = a match {
case "Hello" => "Hello"
case _ => "error"
}