Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在断言异常时,如何知道何时在ScalaTest匹配器中使用'a'、'an'或'the'?_Scala_Scalatest - Fatal编程技术网

在断言异常时,如何知道何时在ScalaTest匹配器中使用'a'、'an'或'the'?

在断言异常时,如何知道何时在ScalaTest匹配器中使用'a'、'an'或'the'?,scala,scalatest,Scala,Scalatest,我正在阅读有关ScalaTest文档的部分,并查看以下示例 an [IndexOutOfBoundsException] should be thrownBy s.charAt(-1) 我测试了a,这也有效 a [IndexOutOfBoundsException] should be thrownBy s.charAt(-1) 及 我很困惑,而且几乎没有关于这些关键字的文档。 谢谢您可以从库中看到源代码a和an完全相同: /** * This method enables the

我正在阅读有关ScalaTest文档的部分,并查看以下示例

an [IndexOutOfBoundsException] should be thrownBy s.charAt(-1)
我测试了a,这也有效

a [IndexOutOfBoundsException] should be thrownBy s.charAt(-1)

我很困惑,而且几乎没有关于这些关键字的文档。
谢谢

您可以从库中看到源代码a和an完全相同:

  /**
   * This method enables the following syntax: 
   *
   * <pre class="stHighlight">
   * a [RuntimeException] should be thrownBy { ... }
   * ^
   * </pre>
   */
  def a[T: ClassTag]: ResultOfATypeInvocation[T] =
    new ResultOfATypeInvocation(classTag)

  /**
   * This method enables the following syntax: 
   *
   * <pre class="stHighlight">
   * an [Exception] should be thrownBy { ... }
   * ^
   * </pre>
   */
  def an[T : ClassTag]: ResultOfAnTypeInvocation[T] =
    new ResultOfAnTypeInvocation(classTag)

  /**
   * This method enables the following syntax: 
   *
   * <pre class="stHighlight">
   * the [FileNotFoundException] should be thrownBy { ... }
   * ^
   * </pre>
   */
  def the[T : ClassTag](implicit pos: source.Position): ResultOfTheTypeInvocation[T] =
    new ResultOfTheTypeInvocation(classTag, pos)
如果您只关心正确的异常类型,请使用a/an。如果需要检查异常类型及其消息,请使用

如果您关心代码中的语法,由于这些匹配器的字符串表示,生成的每个测试失败都会有稍微不同的文本:

// from ResultOfATypeInvocation:
override def toString: String = "a [" + clazz.getName + "]"
vs


例如。

从库中可以看到,源代码a和an完全相同:

  /**
   * This method enables the following syntax: 
   *
   * <pre class="stHighlight">
   * a [RuntimeException] should be thrownBy { ... }
   * ^
   * </pre>
   */
  def a[T: ClassTag]: ResultOfATypeInvocation[T] =
    new ResultOfATypeInvocation(classTag)

  /**
   * This method enables the following syntax: 
   *
   * <pre class="stHighlight">
   * an [Exception] should be thrownBy { ... }
   * ^
   * </pre>
   */
  def an[T : ClassTag]: ResultOfAnTypeInvocation[T] =
    new ResultOfAnTypeInvocation(classTag)

  /**
   * This method enables the following syntax: 
   *
   * <pre class="stHighlight">
   * the [FileNotFoundException] should be thrownBy { ... }
   * ^
   * </pre>
   */
  def the[T : ClassTag](implicit pos: source.Position): ResultOfTheTypeInvocation[T] =
    new ResultOfTheTypeInvocation(classTag, pos)
如果您只关心正确的异常类型,请使用a/an。如果需要检查异常类型及其消息,请使用

如果您关心代码中的语法,由于这些匹配器的字符串表示,生成的每个测试失败都会有稍微不同的文本:

// from ResultOfATypeInvocation:
override def toString: String = "a [" + clazz.getName + "]"
vs

例如。

不幸的是,关于这一点没有那么详细,但大致是这样的:

当您只想断言抛出的异常类型,而不是它的内容(如消息、嵌套异常等),但实现和行为在其他方面是相同的时,应该使用

您应该使用when异常类以元音开头-因为英语拼写规则。在幕后,它们由不同的机器和机器表示,但行为实际上是相同的。我所观察到的唯一区别是在故障消息中使用了a和an

当您想要对抛出的异常类型进行断言时,您应该使用,并且还应该捕获异常实例以对消息、嵌套异常等执行其他断言。

不幸的是,没有那么详细,但大致如下所示:

当您只想断言抛出的异常类型,而不是它的内容(如消息、嵌套异常等),但实现和行为在其他方面是相同的时,应该使用

您应该使用when异常类以元音开头-因为英语拼写规则。在幕后,它们由不同的机器和机器表示,但行为实际上是相同的。我所观察到的唯一区别是在故障消息中使用了a和an

当您希望对抛出的异常类型进行断言时,应该使用,并且还应该捕获异常实例以对消息、嵌套异常等执行其他断言

// from ResultOfAnTypeInvocation:
override def toString: String = "an [" + clazz.getName + "]"