我有一些类似这样的Java代码:
public interface EventBus{
void fireEvent(GwtEvent<?> event);
}
public class SaveCommentEvent extends GwtEvent<?>{
private finalComment oldComment;
private final Comment newComment;
public SaveCommentEvent(Comment oldComment,Comment newComment){
this.oldComment=oldComment;
this.newComment=newComment;
}
public Comment getOldComment(){...}
public Comment getNewComment(){...}
}
并测试如下代码:
def "...."(){
EventBus eventBus=Mock()
Comment oldComment=Mock()
Comment newCommnet=Mock()
when:
eventBus.fireEvent(new SaveCommentEvent(oldComment,newComment))
then:
1*eventBus.fireEvent(
{
it.source.getClass()==SaveCommentEvent;
it.oldComment==oldComment;
it.newComment==newComment
}
)
}
我希望验证eventBus.fireEvent(..)
被调用一次,触发的事件类型为SaveCommentEvent
,并且构造参数包括oldComment
和newComment
。
代码可以正常运行,但问题是:
在更改了闭包内容之后
{
it.source.getClass()==SaveCommentEvent;
it.oldComment==oldComment; //old==old
it.newComment==newComment //new==new
}
To
:这里缺少具体的上下文信息,无法准确翻译。 {
it.source.getClass()==Other_Class_Literal;
it.oldComment==newComment; //old==new
it.newComment==oldComment //new==old
}
然而,代码并没有出错?显然闭包没有完成我想要的功能,所以问题是:如何进行参数捕获?
;
替换为&&
。(代码参数约束需要根据它们是否匹配返回true
或false
。) - Peter Niederwieser