考虑以下IO:
val io = (for{
userChoice <- getChar
} yield if (userChoice == 'y') putStr("yes") else putStr("no")).flatMap(identity)
我正在尝试像这样测试它:
val input: InputStream = new ByteArrayInputStream(Array('n', 'n')) // Not sure why I need two of them...
System.setIn(input)
val output = new ByteArrayOutputStream()
System.setOut(new java.io.PrintStream(output))
io.unsafePerformIO()
System.setIn(new FileInputStream(FileDescriptor.in))
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)))
output.toString ==== "no"
我正在sbt中使用specs2进行测试。我第一次运行测试,它通过了。第二次失败,Console has reached end of input
并且sbt处于怪异状态,我需要杀死它。
我想可能与sbt有某种形式的交互,但我不知道它到底是什么。有没有更好的方法来测试IO?
对于这种交互,我认为使用Free monad开发一种“迷你语言”是值得的,并且有2个解释器,一个解释器用于使用Id
Monad进行测试,另一个解释器用于使用IO
monad进行生产。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句