如何使用 ScalaTest 测试预期异常的其他属性

发布于 2024-10-03 18:34:00 字数 528 浏览 0 评论 0原文

我正在使用 ScalaTest 来测试一些 Scala 代码。 我目前使用这样的代码测试预期的异常

import org.scalatest._
import org.scalatest.matchers.ShouldMatchers

class ImageComparisonTest extends FeatureSpec with ShouldMatchers{

    feature("A test can throw an exception") {

        scenario("when an exception is throw this is expected"){
            evaluating { throw new Exception("message") } should produce [Exception]
        }
    }
}

,但我想对异常添加额外的检查,例如我想检查异常消息是否包含某个字符串。

有没有一种“干净”的方法来做到这一点?或者我必须使用 try catch 块吗?

I'm using ScalaTest for testing some Scala code.
I currently testing for expected exceptions with code like this

import org.scalatest._
import org.scalatest.matchers.ShouldMatchers

class ImageComparisonTest extends FeatureSpec with ShouldMatchers{

    feature("A test can throw an exception") {

        scenario("when an exception is throw this is expected"){
            evaluating { throw new Exception("message") } should produce [Exception]
        }
    }
}

But I would like to add additional check on the exception, e.g. I would like to check that the exceptions message contains a certain String.

Is there a 'clean' way to do this? Or do I have to use a try catch block?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2024-10-10 18:34:00

我找到了解决方案

val exception = intercept[SomeException]{ ... code that throws SomeException ... }
// you can add more assertions based on exception here

I found a solution

val exception = intercept[SomeException]{ ... code that throws SomeException ... }
// you can add more assertions based on exception here
似狗非友 2024-10-10 18:34:00

您可以使用评估...应该生成语法做同样的事情,因为像拦截一样,它返回捕获的异常:

val exception =
  evaluating { throw new Exception("message") } should produce [Exception]

然后检查异常。

You can do the same sort of thing with the evaluating ... should produce syntax, because like intercept, it returns the caught exception:

val exception =
  evaluating { throw new Exception("message") } should produce [Exception]

Then inspect the exception.

独留℉清风醉 2024-10-10 18:34:00

如果需要进一步检查预期的异常,可以使用以下语法捕获它:

val thrown = the [SomeException] thrownBy { /* Code that throws SomeException */ }

此表达式返回捕获的异常,以便您可以进一步检查它:

thrown.getMessage should equal ("Some message")

您还可以在一条语句中捕获并检查预期的异常,如下所示:

the [SomeException] thrownBy {
  // Code that throws SomeException
} should have message "Some message"

If you need to further inspect an expected exception, you can capture it using this syntax:

val thrown = the [SomeException] thrownBy { /* Code that throws SomeException */ }

This expression returns the caught exception so that you can inspect it further:

thrown.getMessage should equal ("Some message")

you can also capture and inspect an expected exception in one statement, like this:

the [SomeException] thrownBy {
  // Code that throws SomeException
} should have message "Some message"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文