gpt4 book ai didi

spock - 如何使用Spock框架进行参数捕获?

转载 作者:行者123 更新时间:2023-12-03 12:28:03 62 4
gpt4 key购买 nike

我有一些类似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(..)和构造参数 SaveCommentEventoldComment的Event调用了 newComment

代码运行无错误,但问题是:

更改封包内容后

{
it.source.getClass()==SaveCommentEvent;
it.oldComment==oldComment; //old==old
it.newComment==newComment //new==new
}



 {
it.source.getClass()==Other_Class_Literal;
it.oldComment==newComment; //old==new
it.newComment==oldComment //new==old
}

仍然,代码运行没有错误?显然,闭包没有满足我的要求,所以问题是:如何进行参数捕获?

最佳答案

我知道了:

    SaveCommentEvent firedEvent

given:
...

when:
....

then:
1 * eventBus.fireEvent(_) >> {arguments -> firedEvent=arguments[0]}
firedEvent instanceof SaveModelEvent
firedEvent.newModel == newModel
firedEvent.oldModel == oldModel

关于spock - 如何使用Spock框架进行参数捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22111212/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com