单元测试在Grails中承诺任务 [英] Unit testing Promise task in grails

查看:112
本文介绍了单元测试在Grails中承诺任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在Promises.task中测试逻辑单元?

 任务{service.method()} 

我想验证任务内服务方法的调用。



这可能吗?如果是的话,怎么样?



我在文档中看到,在单元测试异步过程中,可以使用这个:

  Promises.promiseFactory = new SynchronousPromiseFactory()

尝试添加它在我的设置,但仍然无法正常工作。

解决方案

长途



我一直在为此付出一些努力。



我尝试了这些:


  1. grails单元测试+线程

  2. 验证指定超时的Spock模拟

还尝试了与您一样的文档解决方案:

  Promises.promiseFactory = new SynchronousPromiseFactory()

所有这些都没有成功。



解决方案

所以我最终得到了 meta classing code> setup 方法,我嘲笑了 Promise.task 闭包,所以它在当前线程中运行闭包,而不是在新的one:

  def setup(){
Promises.metaClass.static.task = {Closure c - > c()}
// ...更多需要的东西...
}

感谢这一点,我可以测试代码,因为它不会使用多线程

即使我很远对此100%满意,目前为止我还没有得到更好的结果。


How do we unit test logic in Promises.task?

task{service.method()} 

I want to validate invocation of the service method inside the task.

Is this possible? If yes, how?

I read in the documentation that in unit testing async processes, one can use this:

Promises.promiseFactory = new SynchronousPromiseFactory()

Tried adding it in my setup, but still does not work.

解决方案

The long way

I've been struggling with this for a moment too.

I tried those:

  1. grails unit test + Thread
  2. Verify Spock mock with specified timeout

Also tried the same solution from the docs as you:

Promises.promiseFactory = new SynchronousPromiseFactory()

All went with no luck.

The solution

So I ended up with meta classing.

In the test's setup method, I mocked the Promise.task closure, so it runs the closure in the current thread, not in a new one:

def setup() {
    Promises.metaClass.static.task = { Closure c -> c() }
    // ...more stuff if needed...
}

Thanks to that, I can test the code as it wouldn't use multi threading.

Even I'm far from being 100% happy with this, I couldn't get anything better so far.

这篇关于单元测试在Grails中承诺任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆