Skip to content

Instantly share code, notes, and snippets.

@systemist
Created November 8, 2016 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save systemist/29d4fa745fd245ec7ebafceb7018772e to your computer and use it in GitHub Desktop.
Save systemist/29d4fa745fd245ec7ebafceb7018772e to your computer and use it in GitHub Desktop.
RSpec.describe "Using a table for multiple examples" do
def self.define_data_table(name, table_doc)
fields, *records = *table_doc.lines.map { |l| l.split('|').map(&:strip).reject(&:empty?) }
define_singleton_method(name) { records.map { |r| Hash[fields.map(&:to_sym).zip(r)] } }
end
define_data_table(:users, <<~USERS)
| name | email | twitter |
| Aslak | aslak@cucumber.io | @aslak_hellesoy |
| Julien | julien@cucumber.io | @jbpros |
| Matt | matt@cucumber.io | @mattwynne |
USERS
users.each do |user|
it "should have the correct email for #{user[:name]}" do
expect(user[:email]).to eq("#{user[:name].downcase}@cucumber.io")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment