Ruby Cucumber 带有插值的多行引号?

发布于 2024-10-06 14:30:25 字数 879 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随风而去 2024-10-13 14:30:25

更改为 ERB 语法 (<%= ... %>),然后在步骤定义中通过 ERB 运行字符串:

require 'erb'

When %r{^I POST (.+) with data:$} do |path, data_str|
  data = ERB.new(data_str).result(binding)
  # ...
end

Change to ERB syntax (<%= ... %>), and then in your step definition, run the string through ERB:

require 'erb'

When %r{^I POST (.+) with data:$} do |path, data_str|
  data = ERB.new(data_str).result(binding)
  # ...
end
作业与我同在 2024-10-13 14:30:25

ERB 是推迟评估的一种方法,但西奥,这也许更干净一点?

其中的两半是场景侧:

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:
   # 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

ERB is one way to defer evaluation, but perhaps, Theo, this is a little cleaner ?

The two halves of this are the scenario side:

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:
   # outer, single quotes defer evaluation of #{@document}
   '{
     "documentId":"#{@document.id}",
     "foo":"bar",
     "etc":"etc" 
   }'
 Then check things

And the step definition side:

When %r{^I POST (.+) with data:$} do |path, data_str|
  # assuming @document is in scope...
  data = eval(data_str)
  # ...
end
尾戒 2024-10-13 14:30:25

我建议使用场景大纲和示例,使用类似

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

I would recommend using scenario outlines and examples using something like

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     |

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文