我想测试,当一个协作者接收到方法调用Mockito将接受验证,如果只有某个属性被正确设置。因此,逻辑是这样的:使用Hamcrest hasProperty与Specs2/Mockito
- 下测试类调用一个合作者的说法
- 下测试类构造一个新的对象传递给合作者
- 的Mockito验证进行呼叫,如果传递的对象具有特定的属性设置
代码明智的什么我到目前为止是:
class ExampleSpec extends Specification with Mockito with Hamcrest {
val collaborator = mock[TargetObject]
"verifying a mock was called" should {
"match if a field on the called parameter was the same" in {
val cut = new ClassUnderTest(collaborator)
cut.execute();
there was one(collaborator).call(anArgThat(hasProperty("first", CoreMatchers.equalTo("first"))))
}
}
}
w ^这里定义的类是:
class ClassUnderTest(collaborator: TargetObject) {
def execute() =
collaborator.call(new FirstParameter("first", "second"))
}
class FirstParameter(val first: String, val second: String) {
}
trait TargetObject {
def call(parameters: FirstParameter)
}
在香草Java的我会做到这一点无论是与Hamcrest hasProperty匹配(如上试过)或通过实现我自己的FeatureMatcher提取我想要的领域。以上错误代码如下:
java.lang.Exception: The mock was not called as expected:
Argument(s) are different! Wanted:
targetObject.call(
hasProperty("first", "first")
);
-> at example.ExampleSpec$$anonfun$1$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(ExampleSpec.scala:18)
Actual invocation has different arguments:
targetObject.call(
FirstParameter(first,second)
);
诊断并没有真正地告诉我很多。有没有办法做到这一点,我想如何用Hamcrest匹配器,或理想情况下更符合Specs2做到这一点?
多数民众赞成真棒谢谢;我的真实数据是使用一个选项而不是一个直接的值。你知道我该如何解决这个问题吗? – tddmonkey
有像'beSome(value)'或'beSome(startWith(“s”))或'beSome((v:String)=> v必须有size(2))'或'beNone'这样的'Option'的匹配器。 – Eric