Skip to content

Instantly share code, notes, and snippets.

@r00k
Last active August 29, 2015 14:07
Show Gist options
  • Save r00k/d14ac95fcc1949268796 to your computer and use it in GitHub Desktop.
Save r00k/d14ac95fcc1949268796 to your computer and use it in GitHub Desktop.
##
# Helpers for randomized person testing.
module PersonsHelper
##
# Generate the next n elements for the array, but in a predefined manner.
class FakeElementGenerator
##
# Generate elements, the first of which must be start_with.
def initialize(start_with:)
@start = start_with
end
##
# Generate n elements. If start_with does not cover it, use FactoryGirl to
# make the rest.
def generate(n, excluded_office:)
diff = n - @start.length
if diff > 0
@start + FactoryGirl.create_list(:person, diff)
else
@start.first(n)
end
end
end
##
# The first "randomly"-generated objects will be the ones specified as
# start.
def stack_randomizer(start)
fake_generator = FakeElementGenerator.new(start_with: start)
Game.element_generator = fake_generator
end
end
RSpec.configure do |config|
config.include PersonsHelper
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment