2016-07-04 93 views
0

在Concordion中編寫測試規範時,有時需要在腳本中包含調用的輸出。例如,我想通過發佈一個新對象來測試一個REST服務,然後驗證返回的對象是否包含它自己的URI字符串。在這種情況下,我認爲URI字符串的格式被包含在測試腳本中而不是埋在燈具中是正確的。在Concordion markdown中使用變量

假設名爲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沒有得到解決。你會推薦什麼方法?

回答

0

我建議在規範中聲明URI字符串的靜態格式,而不是實際的值(這是動態的,每次都會導致不同的規範)。

燈具可以比較預期格式和實際格式。該技術在instrumentation documentation的日期轉換的上下文中進行了描述。

使用這種技術,你的降價可以寫成:

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; }

成功時,輸出的文檔會顯示:

successful example

,失敗:

enter image description here

+0

這是巧妙。我今天正在做別的事情,但會盡快嘗試。唯一的區別是'product'不是一個String,而是一個POJO(一個'Product'類的實例,它將通過一個'XStream'實例從XML轉換而來),所以獲得實際URI的方法將會不一樣。 –

+0

這就像一個魅力。非常感謝! –