代码之家  ›  专栏  ›  技术社区  ›  Nek

如何将黄瓜步骤定义与表一起用于最后一个参数?

  •  7
  • Nek  · 技术社区  · 14 年前

    此代码:

    Then %{I should see the following data in the "Feeds" data grid:
                                                       |   Name   |
                                                       | #{name}  |}
    

    Then "I should see the following data in the \"Feeds\" data grid:
    |   Name   |
    | #{name}  |"
    

    而这个:

      Then "I should see the following data in the \"Feeds\" data grid:\n|   Name   |\n| #{name}  |"
    

    甚至这个:

    Then <<EOS
    I should see the following data in the "Feeds" data grid:
    |   Name   |
    | #{name}  |
    EOS
    

    给我:

    Your block takes 2 arguments, but the Regexp matched 1 argument.
    (Cucumber::ArityMismatchError)
      tests/endtoend/step_definitions/instruments_editor_steps.rb:29:in `/^the editor shows "([^"]*)" in the feeds list$/'
      melomel-0.6.0/lib/melomel/cucumber/data_grid_steps.rb:59:in `/^I should see the following data in the "([^"]*)" data grid:$/'
      tests/endtoend/instruments_editor.feature:11:in `And the editor shows "myFeed" in the feeds list
    

    Then "I should see the following data in the \"Feeds\" data grid: |   Name   || #{name}  |"
    

    还有这个:

    Then "I should see the following data in the \"Feeds\" data grid:|   Name   || #{name}  |"
    

    给予:

    Undefined step: "I should see the following data in the "Feeds" data grid:|   Name   || myFeed  |" (Cucumber::Undefined)
      ./tests/endtoend/step_definitions/instruments_editor_steps.rb:31:in `/^the editor shows "([^"]*)" in the feeds list$/'
      tests/endtoend/instruments_editor.feature:11:in `And the editor shows "myFeed" in the feeds list'
    
    4 回复  |  直到 8 年前
        1
  •  6
  •   Nek    14 年前

    我自己找到了答案:

    steps %Q{
    Then I should see the following data in the "Feeds" data grid:
                                                    |   Name   |
                                                    | #{name}  |
    }
    
        2
  •  3
  •   Ben    14 年前

    注意:可能看起来很明显,但是第一个{后面的新行非常重要

    另一种方式:

    Given /^My basic step:$/ do |table|
      #do table operation
    end
    
    
    Given /^My referring step:$/ do |table|
      table.hashes.each do |row|    
        row_as_table = %{
          |prop1|prop2|
          |#{row[:prop1]}|#{row[:prop2]}|
        }
        Given %{My basic step:}, Cucumber::Ast::Table.parse(row_as_table, "", 0)
      end
    end
    
        3
  •  3
  •   nowk    14 年前

    你也可以这样写,用 #table

    Then /^some other step$/ do
      Then %{I should see the following data in the "Feeds" data grid:}, table(%{
        | Name    |
        | #{name} |
      })
    end
    
        4
  •  0
  •   user132837    13 年前

    考虑使用

    Given /^events with:-$/ do |table|
      Given %{I am on the event admin page}
      table.hashes.each do |row|
        Given %{an event with:-}, Cucumber::Ast::Table.new([row]).transpose
      end
    end
    

    我觉得手工搭桌子更优雅。

    | Form | Element | Label |
    | foo  | bar     | baz   |
    

    还有一个事件有:-得到一个像

    | Form    | foo |
    | Element | bar |
    | Label   | baz |