gpt4 book ai didi

scala - 如何匹配抛出的异常消息?

转载 作者:行者123 更新时间:2023-12-04 14:45:35 25 4
gpt4 key购买 nike

The specs2 Matchers guide状态:

throwA[ExceptionType](message = "boom") additionally checks if the exception message is as expected



但是当我使用它时,该消息显然与整个堆栈跟踪匹配,而不仅仅是异常消息。

测试
"cont'd what if -- running test again shows that the app has already died" in {
running(new FakeApplication(additionalConfiguration = inLocalPostgresDatabase())) {
db2 withSession {
val comboboxOpsClass = new ComboboxOps(database)
}
} must throwA[SQLException](message = "Attempting to obtain a connection from a pool that has already been shutdown")
}

堆栈跟踪
[error]      'Attempting to obtain a connection from a pool that has already been shutdown. 
[error] Stack trace of location where pool was shutdown follows:
[error] java.lang.Thread.getStackTrace(Thread.java:1568)
[error] com.jolbox.bonecp.BoneCP.captureStackTrace(BoneCP.java:572)
[error] com.jolbox.bonecp.BoneCP.shutdown(BoneCP.java:161)

很多很多线
[error]       org.specs2.execute.ResultExecution$class.execute(ResultExecution.scala:22)
[error] org.specs2.execute.ResultExecution$.execute(ResultExecution.scala:116)
[error] org.specs2.specification.FragmentExecution$class.executeBody(FragmentExecution.scala:28)
[error]
[error] sbt.ForkMain$Run.runTestSafe(ForkMain.java:211)
[error] sbt.ForkMain$Run.runTests(ForkMain.java:187)
[error] sbt.ForkMain$Run.run(ForkMain.java:251)
[error] sbt.ForkMain.main(ForkMain.java:97)
[error] ' doesn't match '.*Attempting to obtain a connection from a pool that has already been shutdown.*' (FakeApplicationSpec.scala:138)

有人可以指出我使用 specs2 的一个工作示例吗?

最佳答案

我不想这么说,但我从来没有找到一种通用的方法来发现文本是什么,除了让它发生并在事后添加它......或者去异常的来源并从那里复制它。这是我在 Novus-JDBC 中所做的,第 91 行:

"handle when take more than it can give" in{
val iter0 = nonCounter()

val iter = iter0 slice (0,10)

(iter next () must be greaterThan 0) and
(iter next () must be greaterThan 0) and
(iter next () must be equalTo -1) and
(iter next () must be equalTo 3) and
(iter.hasNext must beFalse) and
(iter next() must throwA(new NoSuchElementException("next on empty iterator")))
}

关于scala - 如何匹配抛出的异常消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21030053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com