【问题标题】:Inject mock with doWithSpring使用 doWithSpring 注入模拟
【发布时间】:2018-03-08 12:21:06
【问题描述】:

在 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 中,它似乎不再起作用了。 并且已经从测试文档中删除了它的提及。

有什么方法可以再次实现这种行为吗?

提前非常感谢!

/布莱恩

【问题讨论】:

  • 您的测试类是否实现了GrailsUnitTest 特征之一?因为在 GrailsUnitTest 中进行现有 bean 的连接。

标签: unit-testing grails spock grails-3.3


【解决方案1】:

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)
        }
    }

【讨论】:

  • 工作就像一个魅力。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
相关资源
最近更新 更多