Mic*_*ael 0 unit-testing scala mocking specs2
假设我有一个功能def bar(y: Int, f: Int => Int): Int.现在我想测试bar使用specs2并确保f被调用.
如何在没有可变变量的情况下在功能上做到这一点.我可以不用嘲笑吗?
var called = false
bar(1, i => { called = true; i + 1 })
assert(called)
Run Code Online (Sandbox Code Playgroud)
编辑:
如何在功能上做到这一点?
假设bar是纯粹的:
val bar1 = bar(1, i => 0)
val bar2 = bar(1, i => 1)
assert(bar1 != bar2)
Run Code Online (Sandbox Code Playgroud)
但是如果它bar是不纯的,或者如果它没有使用f它的输出结果,则不可能以f功能方式测试调用.实际上,根据纯度的定义,在调用和现在调用纯函数之间没有可观察到的差异.