Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Data-Driven tests in RSpec
class SomeClassUnderTest
def some_method_under_test(input)
input.to_s
end
end
RSpec.describe SomeClassUnderTest do
describe '#some_method_under_test' do
subject { described_class.new.some_method_under_test input_value }
[
[ 'a string', '123', '123.00' ],
[ 'an integer', 123, '123.00' ],
[ 'a float', 123.0, '123.00' ],
[ 'a negative number', -123, '-123.00' ],
[ 'nil', nil, 'nil' ],
].each do |variation, input, expected_output|
describe 'when the input is #{variation}' do
let(:input_value) { input }
it { is_expected.to eq(expected_output) }
end
end
describe 'when the input is invalid' do
let(:input_value) { 'invalid' }
it { expect { subject }.to raise_error }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment