在使用Concordion编写测试规范时,您有时希望在脚本中包含调用的输出。例如,我想通过向REST服务发布一个新对象,然后验证返回的对象是否包括它自己的URI字符串来测试该服务。在这种情况下,我认为URI字符串的格式应该包含在测试脚本中,而不是隐藏在fixture中。
假设已经以某种方式创建了一个名为newProduct的对象,我想编写如下代码:
When I [post a new product](- "#response=post(#newProduct)")<br/>
Then a [product record](- "#product=getContent(#response)") is returned<br/>
and its [id](- "c:set=#productId=getId(#product)") is [ ](- "c:echo=#productId)")<br/>
and its HAL reference matches [products/#productId](- "?=getHalRef(#product)")不幸的是,最后一行中的变量productId没有被解析。你会推荐哪种方法?
发布于 2016-07-05 19:59:47
我建议在规范中说明URI字符串的静态格式,而不是实际值(这些值是动态的,每次都会导致不同的规范)。
夹具可以比较预期格式和实际格式。此技术在instrumentation documentation中的日期转换上下文中进行了描述。
使用这种技术,你的Markdown可以写成:
When I [post a new product](- "#response=post(#newProduct)")<br/> Then a [product record](- "#product=getContent(#response)") is returned<br/> with a HAL reference matching [products/#productId](- "?=checkHalRef(#product, #TEXT)") ( _[#productId](- "#productId=getId(#product)") is [ ](- "c:echo=#productId")_ )
使用checkHalRef方法:
public String checkHalRef(String product, String expected) { String halRef = getHalRef(product); String expectedHalRef = expected.replace("#productId", getId(product)); if (halRef.equals(expectedHalRef)) { return expected; } return halRef; }
如果成功,输出文档将显示:

在失败时:

https://stackoverflow.com/questions/38186716
复制相似问题