Skip to content

Instantly share code, notes, and snippets.

@jacobthemyth
Created November 8, 2016 23:42
Show Gist options
  • Save jacobthemyth/29d4fa745fd245ec7ebafceb7018772e to your computer and use it in GitHub Desktop.
Save jacobthemyth/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 | [email protected] | @aslak_hellesoy |
| Julien | [email protected] | @jbpros |
| Matt | [email protected] | @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