【问题标题】:Grails 2.5.5 w/ Spock Integration tests fail when using addTo*()使用 addTo*() 时带有 Spock 集成测试的 Grails 2.5.5 失败
【发布时间】:2017-01-10 23:35:40
【问题描述】:

这让我发疯了。 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 中运行良好。

【问题讨论】:

    标签: grails integration-testing spock


    【解决方案1】:

    这是我必须做的才能让它工作。数据工厂中的 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
        }
    
    }
    

    测试数据工厂

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

    【讨论】:

    • 嗨,我可能混淆了 grails 版本,但你不应该为 grails 2.5.5 扩展 IntegrationSpec 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多