使用addTo *()时,Grails 2.5.5 w / Spock Integration测试失败

时间:2017-01-10 23:35:40

标签: grails integration-testing spock

这让我疯了。 2.4.4我的集成测试全部通过。升级到2.5.5,我得到了这样的错误:

No signature of method: Project.addToMonitorings() is applicable for argument types: (Monitoring) values: [Monitoring : (unsaved)] Possible solutions: getMonitorings()

我似乎无法追踪如何更新集成测试以使它们再次通过。

示例(当前)测试:

class MonitoringServiceSpec extends Specification {
def monitoringService

TestDataFactory f // factory that builds objects so we can use them in other tests

def setup() {
    f = new TestDataFactory()
}

void "results can be limited"() {
    given:
        Project p = f.getProject()
        p.save(flush: true, failOnError: true)

        def params =  new EcosListParams(new GrailsParameterMap ([offset: 0, max:1, sortColumn: 'id', order: 'asc'], null))

    when:
        p.addToMonitorings(f.getMonitoring(p)).save(flush: true, failOnError: true)
        p.refresh()

        def results = monitoringService.getProjectMonitorings(params, p.id)

    then:
        results.totalCount == 2
        results.size() == 1
}

...

}

我在我的应用程序中到处都有这个错误,它有一对多的关系。他们在2.4.4中工作得很好。

1 个答案:

答案 0 :(得分:0)

以下是我必须要做的才能让它发挥作用。数据工厂中的getMonitoring方法已将Project添加到对象中。它必须隐式地执行addTo

import groovy.sql.Sql;
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap

import spock.lang.*

class MonitoringServiceSpec extends Specification {
    def f = new TestDataFactory()
    def proj

    def sql
    def dataSource
    def monitoringService

    def setup() {
        sql = new Sql(dataSource);
        proj = f.getProject().save()
        Monitoring mon1 = f.getMonitoring(proj).save()
        Monitoring mon2 = f.getMonitoring(proj).save()
        // don't need addTo, the getMonitoring method above implicitly adds it to the project
        proj.save(flush: true, failOnError: true).refresh()
    }

    void "results can be limited"() {
        given:
            def params =  new EcosListParams(new GrailsParameterMap ([offset: 0, max:1, sortColumn: 'id', order: 'asc'], null))

        when:
           def results = monitoringService.getProjectMonitorings(params, proj.id)

        then:
            results.totalCount == 2
            results.size() == 1
    }

    void "results can be offset"() {
        given:
            def params1 = new EcosListParams(new GrailsParameterMap ([offset: 0, max:1, sortColumn: 'id', order: 'asc'], null))
            def params2 = new EcosListParams(new GrailsParameterMap ([offset: 1, max:1, sortColumn: 'id', order: 'asc'], null))

        when:
            def results1 = monitoringService.getProjectMonitorings(params1, proj.id)
            def results2 = monitoringService.getProjectMonitorings(params2, proj.id)

        then:
            results1.size() > 0
            results1.id != results2
    }

}

TestDataFactory

Monitoring getMonitoring(Project p) {
    HabitatObjectiveSuccess hos = HabitatObjectiveSuccess.list(max: 1).get(0);

    return new Monitoring(visitDate: new Date(), notes: 'notes', created: new Date(), createdBy: getPerson(),
            lastUpdated: new Date(), lastUpdatedBy: getPerson(), maintActivitiesOccurring: 2,
            maintActivitiesOccurrText: 'maintActivitiesOccurrText', landownerObjectivesMet: 1,
            landownerObjectivesMetText: 'landownerObjectivesMetText', speciesObjectivesMet: 1,
            speciesObjectivesMetText: 'speciesObjectivesMetText', habitatObjectiveSuccess: hos,
            habitatObjectiveSuccessText: 'habitatObjectiveSuccessText', project: p
        )
    }