"aka" 的 Scala Specs2 匹配器不起作用
Scala Specs2 Matchers with "aka" doesn't work
看起来像这样的 Scala Specs2 匹配器(例如):
def haveSizeOf(size: Int): Matcher[ProductElement] = {
productElement: ProductElement =>
val sizeOfproductElement = productElement.size
sizeOfproductElement aka "size of product element"
} ^^ beEqualTo(size)
并在代码中执行:
updatedProductElement must haveSizeOf(1)
抛出错误:
java.lang.Exception:
'org.specs2.matcher.ThrownExpectations$$anon@6a3b7968'
is not equal to
'1'
我应该做些什么不同的事情?
编辑:
如果删除aka
,则测试成功:
def haveSizeOf(size: Int): Matcher[ProductElement] = {
productElement: ProductElement =>
productElement.size
} ^^ beEqualTo(size)
beEqualTo()
将一个值(如 size
)与 Any
值进行比较,包括 org.specs2.matcher.ThrownExpectation
,这是您使用 aka
构建的值。构建 haveSizeOf
匹配器的正确方法是
def haveSizeOf(size: Int): Matcher[ProductElement] = {
productElement: ProductElement =>
val sizeOfproductElement = productElement.size
beEqualTo(size).apply(sizeOfproductElement aka "size of product element")
}
每个 Matcher[T]
都有一个 apply
方法,它接受 Expectation[T]
类型的值(基本上期望是 T
类型的值加上一个可选的描述你用 aka
).
构建
另一种构建相同匹配器但不重复使用 beEqualTo
的方法是
def haveSizeOf(size: Int): Matcher[ProductElement] = {
productElement: ProductElement =>
(productElement.size == size,
s"the size of product element ${productElement.size} is not $size")
}
看起来像这样的 Scala Specs2 匹配器(例如):
def haveSizeOf(size: Int): Matcher[ProductElement] = {
productElement: ProductElement =>
val sizeOfproductElement = productElement.size
sizeOfproductElement aka "size of product element"
} ^^ beEqualTo(size)
并在代码中执行:
updatedProductElement must haveSizeOf(1)
抛出错误:
java.lang.Exception: 'org.specs2.matcher.ThrownExpectations$$anon@6a3b7968'
is not equal to
'1'
我应该做些什么不同的事情?
编辑:
如果删除aka
,则测试成功:
def haveSizeOf(size: Int): Matcher[ProductElement] = {
productElement: ProductElement =>
productElement.size
} ^^ beEqualTo(size)
beEqualTo()
将一个值(如 size
)与 Any
值进行比较,包括 org.specs2.matcher.ThrownExpectation
,这是您使用 aka
构建的值。构建 haveSizeOf
匹配器的正确方法是
def haveSizeOf(size: Int): Matcher[ProductElement] = {
productElement: ProductElement =>
val sizeOfproductElement = productElement.size
beEqualTo(size).apply(sizeOfproductElement aka "size of product element")
}
每个 Matcher[T]
都有一个 apply
方法,它接受 Expectation[T]
类型的值(基本上期望是 T
类型的值加上一个可选的描述你用 aka
).
另一种构建相同匹配器但不重复使用 beEqualTo
的方法是
def haveSizeOf(size: Int): Matcher[ProductElement] = {
productElement: ProductElement =>
(productElement.size == size,
s"the size of product element ${productElement.size} is not $size")
}