Grails spock单元测试,Stubbing方法返回空列表值

时间:2014-07-09 07:01:29

标签: grails spock

我有spock单元测试,并尝试使用服务方法存根来返回域对象列表,下面是在setup fixture中做了什么:

def themeServiceMock = Mock(ThemeService)

themeServiceMock.getTheme(*_) >> {
   return [new Theme(name:'Family', slug:'family'), new Theme(name:'Luxury', slug:'luxury')]
}

此服务是Mock()编辑到另一个规范的服务。

当我的测试功能出现时,当调用spec的服务getTheme()时,它调用themeservice.getTheme(),并返回一个具有空值的Theme对象列表。任何建议都表示赞赏。

注意:我也试过模拟主题,但也没有工作,在这种情况下,它返回空列表

3 个答案:

答案 0 :(得分:0)

尝试:

def themeServiceMock = Mock(ThemeService) {
   getTheme(_) >> [new Theme(name:'Family', slug:'family'), new Theme(name:'Luxury', slug:'luxury')]
}

答案 1 :(得分:0)

当我在spock中模拟方法调用时,我通常会这样做:

themeServiceMock.getTheme(*_) >> [new Theme(name:'Family', slug:'family'), new Theme(name:'Luxury', slug:'luxury')]

你试过吗?

答案 2 :(得分:0)

哦,男人哦,男人.....不知道出了什么问题,但这就是我修复它的原因:

我刚改变了:

return [new Theme(name:'Family', slug:'family'), new Theme(name:'Luxury', slug:'luxury')]

def t1 = new Theme()
t1.name="Family"
t1.slug = "family"
def t2 = new Theme()
t2.name="Luxury"
t2.slug="luxury"
return [t1, t2]