【问题标题】:Grails async integration testingGrails 异步集成测试
【发布时间】:2017-08-09 12:26:06
【问题描述】:

我有一个带有休息控制器的 Grails 2.5.6 项目,我在其中创建了 n 个异步任务,每个任务都调用服务中的一个方法,如下所示:

// MyController
...
def statuses = ['where', 'ownership', 'store']
def tasks = statuses.collect { st ->
    task {
        return myService.invokeMethod(st, [aDomain, data])
    }
}
def props = waitAll(tasks)
...


// MyService
...
store(aDomain, data) {
    ...
    def store = Store.get(data.store)
    ...
}

如果我执行应用程序,则可以在数据库中正确找到 Store。

我还创建了一个集成测试。为了避免与数据库中的现有数据发生冲突,我在测试期间创建了一个新的Store

// Integration test (simplified)
....
def store = new Store(....)
store.save(flush: true)
...
def json = [store: store.id] as JSON
...
controller.request.content = params.toString()
controller.request.method = "POST"
controller.update()
...

如果我执行测试,则在服务中找不到创建的新商店并且测试失败。 我已经验证了应用程序某些方面的情况并发现: - 如果我在控制器中搜索存储(在任务执行之前或之后),它被发现 - 如果我在服务方法中列出所有商店,则新商店不存在。

我认为这种行为是由于在测试期间如何处理休眠会话,但我不知道如何解决它。 欢迎提出任何建议。

谢谢

【问题讨论】:

    标签: grails


    【解决方案1】:

    尝试使用 Synchronouse 承诺工厂进行测试。

    void setup() { 
      Promises.promiseFactory = new SynchronousPromiseFactory()
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多