所以我有一些代码:
GrailsMock mockDisplayService
GrailsMock mockSessionUserService
def setup() {
mockDisplayService = mockFor(DisplayService)
mockSessionUserService = mockFor(SessionUserService)
controller.DisplayService = mockDisplayService.createMock()
controller.sessionUserService = mockSessionUserService.createMock()
defineBeans{
mockDisplayService(InstanceFactoryBean, mockDisplayService.createMock(), DisplayService)
sessionUserService(InstanceFactoryBean, mockSessionUserService.createMock(), SessionUserService)
}
}
我相信controller.DisplayService = mockDisplayService.createMock()在做什么,是在我们的单元测试期间使用模拟显示服务(模拟类)而不是原始DisplayService来应答从控制器发出的对DisplayService的调用。我的问题是defineBeans的作用是什么?没有在任何地方声明sessionUserService怎么创建一个bean?我们在哪里注入豆子?等等