[英]Ruby Cucumber multi-line quotes with interpolation?
我正在使用Cucumber將JSON發送到一些API操作。 在一個實例中,我需要知道在API調用之前構建的對象的ID,並將該ID傳入。
我想做這個:
Scenario: Creating a print from an existing document
Given I am logged in as "foo@localhost.localdomain"
And I have already built a document
When I POST /api/prints with data:
"""
{
"documentId":"#{@document.id}",
"foo":"bar",
"etc":"etc"
}
"""
Then check things
這是行不通的,因為"""
字符串不會像雙引號字符串那樣對變量進行插值。 I have already built a document
步驟,用於構建@document
對象,所以我不提前知道如果很重要,我將MongoDB與monongoid一起使用,而我手動設置ID的努力被證明是徒勞的。
有沒有一種干凈的方法可以做到這一點?
環境:
ruby: 1.8.7
rails: 3.0.1
cucumber: 0.9.4
cucumber-rails: 0.3.2
更改為ERB語法( <%= ... %>
),然后在步驟定義中,通過ERB運行字符串:
require 'erb'
When %r{^I POST (.+) with data:$} do |path, data_str|
data = ERB.new(data_str).result(binding)
# ...
end
ERB是推遲評估的一種方法,但也許Theo這樣做更清潔一些?
其中的兩半是方案方面:
Scenario: Creating a print from an existing document
Given I am logged in as "foo@localhost.localdomain"
And I have already built a document
When I POST /api/prints with data:
# outer, single quotes defer evaluation of #{@document}
'{
"documentId":"#{@document.id}",
"foo":"bar",
"etc":"etc"
}'
Then check things
而步驟定義方面:
When %r{^I POST (.+) with data:$} do |path, data_str|
# assuming @document is in scope...
data = eval(data_str)
# ...
end
我建議使用方案大綱和示例,例如
Scenario Outline: Posting stuff
....
When I POST /api/prints with data:
"""
{
"documentId": <document_id>,
"foo":"bar",
"etc":"etc"
}
"""
Then check things
Examples: Valid document
| document_id |
| 1234566 |
Examples: Invalid document
| document_id |
| 6666666 |
在示例中。 這樣就可以清楚地知道這些值至少來自哪里。 在此處檢查方案概述中的替換http://cukes.info/step-definitions.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.