Last active
April 22, 2024 13:36
-
-
Save jeantessier/c514cbb2071bf7badbc6356fc4210313 to your computer and use it in GitHub Desktop.
Data-Driven tests in RSpec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeClassUnderTest | |
def some_method_under_test(input) | |
input.to_s | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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