unit-testing - Groovy Spock BlockingVariable 从未发布
问题描述
在我的 Grails 应用程序中,我正在与 Spock 单元测试进行一场失败的战斗。我想测试异步行为,为了熟悉 Spock,BlockingVariable
我编写了这个简单的示例测试。
void "test a cool function of my app I will not tell you about"() {
given:
def waitCondition = new BlockingVariable(10000)
def runner = new Runnable() {
@Override
void run() {
Thread.sleep(5000)
waitCondition.set(true)
}
}
when:
new Thread(runner)
then:
true == waitCondition.get()
}
不幸的是,这不是一件好事,否则它就会结束。当我设置断点Thread.sleep()
并调试测试时,该断点永远不会被命中。我错过了什么?
解决方案
您的测试被破坏了,因为您实际上并没有运行您创建的线程。反而:
when:
new Thread(runner)
你应该做:
when:
new Thread(runner).run()
然后您的测试在大约 5 秒后成功。
推荐阅读
- python - Python Selenium 硬网络抓取
- flutter - Flutter web:连接服务器进行调试
- python - 托管在应用程序引擎上的 Python 应用程序无法与在本地工作的 Firestore 连接
- php - 如何从字符串变量解析 php 函数?
- flutter - RangeError(索引):无效值:不在包含范围 0..2:3
- python - Python迭代和修改列表逻辑
- python - 已解决的依赖项中有不兼容的版本
- c - 为什么我的代码正在打印从文件中检索到的最后一个值而不是新值
- c# - 在 UWP 中单击汉堡菜单项时如何提高应用程序性能?
- r - 如何更改 colsums 和 sum 以在公式中循环?