Mortadella for Ruby

CI Coverage Status

Mortadella for Ruby makes it easy to programmatically build data tables that can be compared to Cucumber for Ruby tables through cucumber_table.diff! mortadella_table.

You want to do this as much as possible. Cucumber for Ruby has very powerful built-in facilities to visualize where and how two tables differ:

Oh no, our algorithm selected too many apples!

Installation

  • add gem 'mortadella' to your Gemfile
  • run bundle install

Usage

Mortadella supports horizontal and vertical Cucumber tables.

Horizontal Tables

  • In your cucumber spec, define the expected data in table form

    Then I have these ingredients
      | INGREDIENT | AMOUNT |
      | flour      | 12 oz  |
      | butter     | 2 oz   |
      | apples     | 3 pc   |
    
  • in the step definition for this, build an equivalent Mortadella table with the actual data, and diff the Cucumber table with the expected data against it.

    Then /^I have these ingredients$/ do |expected_ingredients|
      actual_ingredients = Mortadella::Horizontal.new headers: ['INGREDIENT', 'AMOUNT']
      actual_ingredients << ['flour', '12 oz']   # This data should come from your app
      actual_ingredients << ['butter', '2 oz']   # This data should come from your app
      actual_ingredients << ['apples', '3 pc']   # This data should come from your app
      expected_ingredients.diff! actual_ingredients.table
    end
    
  • you can also dry up repetitive fields for better readability:

    | DAY     | ACTIVITY |
    | Monday  | mowing   |
    |         | musing   |
    | Tuesday | typing   |
    |         | tutoring |
    
  • or filter the columns of your finished table by calling keep_matching_columns

Vertical Tables

  • In your cucumber spec, define the expected data in table form

    Then my pie conforms to these specs:
      | WEIGHT   | 2 lbs |
      | PORTIONS | 8     |
      | CALORIES | 500   |
    
  • in the step definition for this, build an equivalent Mortadella table with the actual data, and diff the Cucumber table with the expected data against it.

    Then /^My pie has these metrics:$/ do |expected_metrics|
      actual_metrics = Mortadella::Vertical.new
      actual_metrics['WEIGHT'] = '2 lbs'   # This data should come from your app
      actual_metrics['PORTIONS'] = 8       # This data should come from your app
      actual_metrics['CALORIES'] = 500     # This data should come from your app
      expected_metrics.diff! actual_metrics.table
    end
    

API Reference

Horizontal Tables

  • new(headers:, dry: []) - Create a new horizontal table with column headers
  • <<(row) - Add a row to the table
  • empty? - Check if the table has no data rows
  • keep_matching_columns(columns) - Filter table to only keep specified columns
  • table - Access the raw Cucumber-compatible table array

Vertical Tables

  • new - Create a new empty vertical table
  • [key] = value - Add a key-value pair to the table
  • empty? - Check if the table has no rows
  • key?(header) - Check if a header exists in the table
  • to_h - Convert the table to a Ruby hash
  • table - Access the raw Cucumber-compatible table array