我正在使用 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
对象,所以我不知道我的 ID 是什么。如果重要的话,我正在使用 MongoDB 和 mongoid,我手动设置 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
关于ruby-on-rails - 带插值的 Ruby Cucumber 多行引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4377828/