1

在 Grails 3.2.x 和更早的版本中,我可以在 spock 单元测试中做这样的事情:

def myServiceMock = Mock(MyService) {
    someMethod() >> 42
}

Closure doWithSpring() {{ ->
    myService(InstanceFactoryBean, myServiceMock, MyService)
}}


def "some test"(){
    expect:
    service.myService.someMethod() == 42
}

这将使模拟能够注入协作类中。

请参阅:http : //docs.grails.org/3.2.4/guide/testing.html 在“doWithSpring 和 doWithConfig 回调方法,FreshRuntime 注释”部分下。

在 Grails 3.3.2 中,它似乎不再起作用了。并且已经从测试文档中删除了它的提及。

有没有办法再次完成这种行为?

提前谢谢了!

/布莱恩

4

1 回答 1

4

Grails 3.3 带有新的测试框架。

在这里您可以找到文档 - https://testing.grails.org/latest/guide/index.html

在 grails 3.3 上运行测试。你可以用这种方式修改你的代码:

def myServiceMock = Mock(MyService) {
    someMethod() >> 42
}

def setup() {
        defineBeans{
            myService(InstanceFactoryBean, myServiceMock, MyService)
        }
    }
于 2018-03-09T05:54:02.147 回答