以下控制器操作在运行应用程序时按预期顺序(按ID减少)返回帖子:
def list() {
PagedResultList posts = Post.createCriteria().list(max: 5, offset: params.offset) {
if (params.searchTag) {
tag {
eq ("name", params.searchTag)
}
}
order("id", "desc")
}
[posts: posts, total: posts.totalCount]
}
但单元测试测试失败,因为帖子未按预期顺序返回:
def “list returns the posts in the correct order”(){
given:
def p1 = new Post(title: 'p1', email: 'aaa@aaa.com', content: 'xx').save()
def p2 = new Post(title: 'p2', email: 'aaa@aaa.com', content: 'xx').save()
when:
def model = controller.list()
then:
p2.id > p1.id
model.posts.iterator().next().title == 'p2' //fails: expected p2 got p1
}
测试失败的原因是什么?