Skip to content

Commit 67e6511

Browse files
authored
Improve #deterministically_verify helper (#2828)
* Ensure that the #deterministically_verify helper works as expected when :random is provided * Prevent the #deterministically_verify helper from mutating the Faker::Config.random state on each :depth iteration * Prevent the #deterministically_verify helper from performing assertions on a generated value against itself * Remove the multi-line block chain from the #deterministically_verify helper implementation * Replace #inject with #map on the #deterministically_verify helper implementation * Use yield instead of &block on the #deterministically_verify helper * Move the default value of :random to the #deterministically_verify method signature * Provide :seed instead of :random instance to the #deterministically_verify helper method * Update #deterministically_verify helper method documentation
1 parent ea21d56 commit 67e6511

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

test/test_helper.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,20 @@
2323
# times with the same deterministic_random seed.
2424
# @param subject_proc [Proc] a proc object that returns the subject under test
2525
# when called.
26-
# @param depth [Integer] the depth of deterministic comparisons to run.
27-
# @param random [Integer] A random number seed; Used to override the default.
26+
# @param depth [Integer] the depth of deterministic comparisons to run; the default value is 2.
27+
# @param seed [Integer] A random number seed; Used to override the default value which is 42.
2828
#
2929
# @example
3030
# deterministically_verify ->{ @tester.username('bo peep') } do |subject|
3131
# assert subject.match(/(bo(_|\.)peep|peep(_|\.)bo)/)
3232
# end
3333
#
34-
def deterministically_verify(subject_proc, depth: 2, random: nil, &block)
35-
raise 'need block' unless block_given?
34+
def deterministically_verify(subject_proc, depth: 2, seed: 42)
35+
results = depth.times.map do
36+
Faker::Config.stub :random, Random.new(seed) do
37+
yield subject_proc.call.freeze
38+
end
39+
end
3640

37-
# rubocop:disable Style/MultilineBlockChain
38-
depth.times.inject([]) do |results, _index|
39-
Faker::Config.random = random || Random.new(42)
40-
results << subject_proc.call.freeze.tap(&block)
41-
end.repeated_combination(2) { |(first, second)| assert_equal first, second }
42-
# rubocop:enable Style/MultilineBlockChain
41+
results.combination(2) { |(first, second)| assert_equal first, second }
4342
end

0 commit comments

Comments
 (0)