Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use regular expressions in a Cucumber table (multiline argument) to diff against table?

I am using a scenario table (multiline step arguments) to check some data from a screen using cucumber, using the in built .diff! method on the Cucumber AST table.

I would like to check the content matches against regular expressions.

Scenario: One
    Then the table appears as:
    | One   | Two   | Three |
    | /\d+/ | /\d+/ | /\d+/ |

The actual table could look something like

| One | Two | Three |
| 123 | 456 | 789   |

which this scenario is translated to "as long as there are some digits, I don't care"

An example step implementation that fails:

Then /^the table appears as:$/ do |expected_table|
  actual_table  = [['One','Two', 'Three'],['123', '456', '789']]
  expected_table.diff! actual_table
end

Error:

Then the table appears as: # features/step_definitions/my_steps.rb:230
      | One    | Two    | Three  |
      | /\\d+/ | /\\d+/ | /\\d+/ |
      | 123    | 456    | 789    |
      Tables were not identical (Cucumber::Ast::Table::Different)

I have tried using step transforms to transform the cells into regular expressions, but they still aren't identical.

Transform code:

 expected_table.raw[0].each do |column|
    expected_table.map_column! column do |cell|
      if cell.respond_to? :start_with?
        if cell.start_with? "/"
          cell.to_regexp
        else
          cell
        end
      else
        cell
      end
    end
  end

which provides the eror:

Then the table appears as: # features/step_definitions/my_steps.rb:228
      | One          | Two          | Three        |
      | (?-mix:\\d+) | (?-mix:\\d+) | (?-mix:\\d+) |
      | 123          | 456          | 789          |
      Tables were not identical (Cucumber::Ast::Table::Different)

Any ideas? I am stuck.

like image 366
Alister Scott Avatar asked Aug 15 '11 04:08

Alister Scott


People also ask

What is the difference between data table and scenario outline?

Scenario Outline is run once for each row in the Examples section beneath it (not counting the first header row). Example tables always have a header row, because the compiler needs to match the header columns to the placeholders in the Scenario Outline's steps.

What is dataTable cucumber?

What is the Data Table in Cucumber? Data tables are used when we need to test numerous input parameters of a web application. For example, the registration form of the new user involves several parameters to test, so for this, we can use the data table.


1 Answers

Using regular expressions in a scenario is almost certainly the wrong approach. Cucumber features are intended to be read and understood by business-focussed stakeholders.

How about writing the step at a higher level, such as as:

Then the first three columns of the table should contain a digit
like image 54
Andy Waite Avatar answered Nov 15 '22 05:11

Andy Waite