org.specs2.mock.Mockito 匹配器未按预期工作 [英] org.specs2.mock.Mockito matchers are not working as expected
问题描述
这是我要运行的代码:
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
import akka.event.LoggingAdapter
class MySpec extends Specification with Mockito {
"Something" should {
"do something" in new Scope {
val logger = mock[LoggingAdapter]
val myVar = new MyClassTakingLogger(logger)
myVar.doSth()
there was no(logger).error(any[Exception], "my err msg")
}
}
}
运行时出现以下错误:
[error] org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
[error] Invalid use of argument matchers!
[error] 2 matchers expected, 1 recorded:
[error] -> at org.specs2.mock.mockito.MockitoMatchers$class.any(MockitoMatchers.scala:47)
[error]
[error] This exception may occur if matchers are combined with raw values:
[error] //incorrect:
[error] someMethod(anyObject(), "raw String");
[error] When using matchers, all arguments have to be provided by matchers.
[error] For example:
[error] //correct:
[error] someMethod(anyObject(), eq("String by matcher"));
这很有意义,但是 eq("my err msg")
和 equals("my err msg")
都没有完成我得到的工作一个错误.我错过了什么?
Which would make a lot of sense, but neither eq("my err msg")
nor equals("my err msg")
does the job as I get an error. What am I missing?
推荐答案
当您使用匹配器来匹配参数时,您必须将它们用于所有参数.正如所有参数必须由匹配器提供
所指示的那样.
When you are using matchers to match parameters you have to use them for all parameters. as the all arguments have to be provided by matchers
indicates.
此外,如果您使用 specs2
匹配器,它需要是强类型的.equals
是一个 Matcher[Any]
但是没有从 Matcher[Any]
到 String
的转换method
接受什么.
Moreover if you use a specs2
matcher it needs to be strongly-typed. equals
is a Matcher[Any]
but there is no conversion from Matcher[Any]
to a String
which is what method
accepts.
所以在你的情况下你需要一个 Matcher[T]
或一个 Matcher[String]
.如果你只是想测试相等性,强类型匹配器是 ===
So you need a Matcher[T]
or a Matcher[String]
in your case. If you just want to test for equality, the strongly-typed matcher is ===
there was no(logger).error(any[Exception], ===("hey"))
这篇关于org.specs2.mock.Mockito 匹配器未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!