在 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
}
这将使模拟能够注入(inject)到协作类中。
参见:http://docs.grails.org/3.2.4/guide/testing.html 在“doWithSpring 和 doWithConfig 回调方法,FreshRuntime 注释”部分下。
在 Grails 3.3.2 中它似乎不再起作用了。 并且它的提及已从测试文档中删除。
有什么办法可以再次实现这种行为吗?
非常感谢!
/布莱恩
最佳答案
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)
}
}
关于unit-testing - 使用 doWithSpring 注入(inject)模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49172980/