When writing a test specification in Concordion, you sometimes want to include the output of a call in the script. For example, I want to test a REST service by posting a new object to it and then verifying that the returned object includes its own URI string. In these circumstances, I think it's right that the format of the URI string is included in the test script rather than buried within a fixture.
Assuming that an object named newProduct
has been created somehow, I would like to write something like this:
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)")
Unfortunately the variable productId
in the last line is not resolved. What approach would you recommend?
I'd recommend stating the static format of the URI string in the specification rather than the actual values (which are dynamic and will lead to a different spec each time).
The fixture can compare the expected and actual formats. This technique is described in the context of date conversion in the instrumentation documentation.
Using this technique, your Markdown could be written as:
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")_ )
with the checkHalRef
method:
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;
}
On success, the output documentation would show:
and on failure: