Ruby Cucumber 带有插值的多行引号?
我正在使用 Cucumber 将 JSON 发送到一些 API 操作。在一种情况下,我需要知道在 API 调用之前构建的对象的 ID 并将该 ID 传入。
我想这样做:
Scenario: Creating a print from an existing document
Given I am logged in as "[email protected]"
And I have already built a document
When I POST /api/prints with data:
"""
{
"documentId":"#{@document.id}",
"foo":"bar",
"etc":"etc"
}
"""
Then check things
这不起作用,因为 """
string 不像双引号字符串那样插入变量。 我已经构建了一个文档
步骤构建了 @document
对象,所以我不知道。如果重要的话,我正在使用 MongoDB 和 mongoid,并且我手动设置 ID 的努力已被证明是徒劳的。
环境
:
ruby: 1.8.7
rails: 3.0.1
cucumber: 0.9.4
cucumber-rails: 0.3.2
I'm using Cucumber to send in JSON to some API actions. In one instance, I need to know the ID of an object that was built prior to the API call and pass that ID in.
I want to do this:
Scenario: Creating a print from an existing document
Given I am logged in as "[email protected]"
And I have already built a document
When I POST /api/prints with data:
"""
{
"documentId":"#{@document.id}",
"foo":"bar",
"etc":"etc"
}
"""
Then check things
Which doesn't work, because the """
string doesn't interpolate variables like a double-quoted string would. The I have already built a document
step builds the @document
object, so I don't know ahead of time what my ID will be. If it matters, I'm using MongoDB with mongoid, and my efforts to manually set an ID have proven fruitless.
Is there a clean way to accomplish this?
Environment:
ruby: 1.8.7
rails: 3.0.1
cucumber: 0.9.4
cucumber-rails: 0.3.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改为 ERB 语法 (
<%= ... %>
),然后在步骤定义中通过 ERB 运行字符串:Change to ERB syntax (
<%= ... %>
), and then in your step definition, run the string through ERB:ERB 是推迟评估的一种方法,但西奥,这也许更干净一点?
其中的两半是场景侧:
和步骤定义侧:
ERB is one way to defer evaluation, but perhaps, Theo, this is a little cleaner ?
The two halves of this are the scenario side:
And the step definition side:
我建议使用场景大纲和示例,使用类似
示例中的内容。这至少可以清楚地表明这些值来自哪里。在此处检查场景大纲中的替换 http://cukes.info/step-definitions.html
I would recommend using scenario outlines and examples using something like
in examples. That would make it clear where the values came from at least. Check Substitution in Scenario Outlines here http://cukes.info/step-definitions.html