【发布时间】:2017-02-13 23:35:56
【问题描述】:
我在 Grails 3.2.5 上运行并实现了一个简单的服务。该服务有一个私有方法和一个公共方法。私有方法触发 EventBus 的 notify 方法(由 Events trait 提供)。
@Transactional
class SyncService {
def processQueue() {
checkStatus(true)
}
private checkStatus(status) {
if(status) {
def model = [...]
notify "status.completed", model
}
}
}
如何为此服务编写单元测试,检查通知是否已触发?以下实现不起作用:
@TestFor(SyncService)
class SyncServiceSpec extends Specification {
void "test if notification is triggerd() {
when:
service.processQueue()
then: "notification should be triggered"
1 * service.notify(_)
}
}
测试失败,输出如下:
Too few invocations for:
1 * service.notify(_) (0 invocations)
感谢您的帮助!
【问题讨论】: