Skip to content

Commit a7129ce

Browse files
author
Kirk Wang
committed
Refactor TestFakerDeprecation
This is a proposed change to the `TestFakerDeprecation` class that makes it easier for developers to add new tests for deprecations. The introduced `DEPRECATION_MAPPINGS` hash allows for devs to add new tests in one location.
1 parent 1b3119f commit a7129ce

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

test/test_faker_deprecator.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,32 @@
33
require_relative 'test_helper'
44

55
class TestFakerDeprecation < Test::Unit::TestCase
6+
##
7+
# A hash that maps deprecated methods to their new counterparts.
8+
#
9+
# @return [Hash{Array<String, String> => Array<String, String>}]
10+
#
11+
# @example Add the following into the hash
12+
# ['Faker::OldKlass', 'old_method'] => ['Faker::NewKlass', 'new_method']
13+
DEPRECATION_MAPPINGS = {
14+
['Faker::IDNumber', 'valid'] => ['Faker::IdNumber', 'valid'],
15+
['Faker::JapaneseMedia::FmaBrotherhood', 'character'] => ['Faker::JapaneseMedia::FullmetalAlchemistBrotherhood', 'character']
16+
}.freeze
17+
618
def test_using_a_deprecated_generator_returns_a_warning_message
719
actual_stdout, actual_stderr = capture_output do
8-
Faker::IDNumber.valid
9-
Faker::JapaneseMedia::FmaBrotherhood.character
20+
DEPRECATION_MAPPINGS.each_key { |klass, method| Object.const_get(klass).send(method) }
1021
end
1122

12-
assert_includes(actual_stdout, 'DEPRECATION WARNING: Faker::IDNumber is deprecated. Use Faker::IdNumber instead')
13-
assert_includes(actual_stdout, 'DEPRECATION WARNING: Faker::JapaneseMedia::FmaBrotherhood is deprecated. Use Faker::JapaneseMedia::FullmetalAlchemistBrotherhood instead')
23+
DEPRECATION_MAPPINGS.each do |(deprecated_klass, _deprecated_method), (new_klass, _new_method)|
24+
assert_includes(actual_stdout, "DEPRECATION WARNING: #{deprecated_klass} is deprecated. Use #{new_klass} instead")
25+
end
1426
assert_empty(actual_stderr)
1527
end
1628

1729
def test_using_a_non_deprecated_generator_does_not_return_a_warning_message
1830
actual_stdout, actual_stderr = capture_output do
19-
Faker::IdNumber.valid
20-
Faker::JapaneseMedia::FullmetalAlchemistBrotherhood.character
31+
DEPRECATION_MAPPINGS.each_value { |klass, method| Object.const_get(klass).send(method) }
2132
end
2233

2334
assert_empty(actual_stdout)

0 commit comments

Comments
 (0)