【发布时间】:2018-09-25 19:06:07
【问题描述】:
我正在使用 Grails 2.5.4 开发一个项目,并且我目前正在尝试运行一些未运行的集成测试。 我已经调试了这个问题,发现在集成测试中运行时,要测试的服务上的一些动态方法显然不存在(如果你在应用程序的上下文中运行,方法就在那里并且一切正常)。这发生在我尝试运行的许多测试中,我选择了一个作为示例,但其他失败的测试也有同样的问题。
我有这个域类
class Event {
...
static hasMany = [
bundles : Bundle
]
...
}
以及要测试的服务方法:
@Transactional
class BundleService {
...
void assignEvent(Event event, List bundleIds) {
..
for (id in bundleIds) {
event.addToBundles(Bundle.get(id))
}
}
...
}
然后我运行这个 spock 测试
class BundleServiceIntegrationSpec extends Specification {
BundleService bundleService
EventService eventService
private BundleTestHelper bundleHelper = new BundleTestHelper()
...
void '04. Test deleteBundleAndAssets method'() {
when: 'a new Bundle is created'
Bundle bundle = bundleHelper.createBundle(project, 'Test Bundle')
and: 'a new Event is created'
Event event = eventService.create(project, 'Test Event')
and: 'the above Bundle is assigned to the Event'
bundleService.assignEvent(event, [bundle.id])
...
}
它在 BundleService 的 moveEvent.addToBundles(Bundle.get(id)) 行中失败,并出现以下异常
groovy.lang.MissingMethodException: No signature of method:
net.domain.Event.addToBundles() is applicable for argument
types: (net.domain.Bundle) values: [Test Bundle]
Possible solutions: getBundles()
at net.service.BundleService.$tt__assignEvent(BundleService.groovy:101)
问题在于,由于 hasMany 集合“bundles”,应该由 Grails 动态添加到 Event 类的方法 addToBundles() 没有添加。正如我所提到的,如果您运行应用程序并使用此服务,方法就在那里,一切正常。
我尝试更改测试的基类(从 Specification 到 IntegrationSpec),因为我相信这里是管理动态功能以及事务管理和其他用于集成测试的东西的地方,但它没有奏效。
是否有任何理由为什么服务中应该存在的这种方法在集成测试的上下文中不存在?谢谢
【问题讨论】:
标签: grails integration-testing spock grails-2.5