【发布时间】:2014-10-13 17:30:11
【问题描述】:
我有一个 grails 函数,我在其中使用单独的线程:
def testFunction() {
//.....
Thread.start() {
testService.somefunction()
}
//...
}
在单元测试中,我像这样模拟服务功能:
def "test testfunction" {
//...
1 * testService.somefunction(_)
//..
}
但是,我收到了不匹配的调用错误,因为 Spock 没有检测到该方法是在单独的线程上执行的。
1 * testService.somefunction(_) (0 invocations)
Unmatched invocations (ordered by similarity):
我尝试使用这个http://spock-framework.readthedocs.org/en/latest/new_and_noteworthy.html#polling-conditions,但没有任何成功。
更新以包含代码示例:
void "test without errors"() {
def conditions = new PollingConditions(timeout: 15)
def cmdList = new ArrayList<CommandClass>()
parseService.parseFile(file, _) >> commandList
nextService.create(_) >> commandList
controller.controllerService = nextService
controller.controllerParseService = parseService
when:
controller.testFunction()
then:
conditions.eventually {
assert response.contentAsString == "SUCCESS"
}
}
【问题讨论】:
-
您能否输入您的确切测试代码而不是“//...”。这可能有助于确定问题所在。
-
@th3morg,这是我觉得保密的事情,但我相信上面的 sn-p 会给出这样的好主意。问题在于
Thread.start{}中的代码 -
如果没有看到整个测试,我无法判断您是否正确配置了测试。您甚至可以只提供完整的测试代码,而不是专有的功能。另外,是否有任何无与伦比的调用?
-
@th3morg,我已经更新了我的代码
-
@th3morg,我不知道确切的原因,但现在单元测试正在工作,真的不知道这是我的项目中的一个漏洞或 spock 中的错误,从一开始这个 sn -p 显示一些不寻常的行为