Last active
July 9, 2024 15:37
-
-
Save trueheart78/ef9f6903d42dc2a2bcd4a38be571f10e to your computer and use it in GitHub Desktop.
Revisions
-
trueheart78 revised this gist
Jul 9, 2024 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ # All credit to Dave Brady & his RSpec-Junklet Project that changed how I think about test data. # * https://rubygems.org/profiles/dbrady # Place in rspec/support/junklet.rb and have the rails_helper.rb load it # Returns randomized data for when we don't care *what* the data is. def junk(length = 32) -
trueheart78 created this gist
Jul 9, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ # All credit to Dave Brady & his RSpec-Junklet Project that changed how I think about test data. # https://rubygems.org/profiles/dbrady # Returns randomized data for when we don't care *what* the data is. def junk(length = 32) # hex returns length*2 digits, because it returns a 0..255 byte # as a hex pair. But when we want junk, we want *bytes* of # junk. Get (length+1)/2 chars, which will be correct for even # sizes and 1 char too many for odds, so trim off with [0...length] SecureRandom.hex((length + 1) / 2)[0...length] end # Use in context and describe blocks. # Example: # junklet :random_one, :random_two, :random_forty_two def junklet(*args) opts = args.size > 1 && !args.last.is_a?(Symbol) && args.pop || {} names = args.map(&:to_s) separator = opts[:separator] || "_" names = names.map {|name| name.gsub(/_/, separator) } args.zip(names).each do |arg, name| make_junklet arg, name, separator end end # Makes the magic happen for the junklet call in describe and context blocks. def make_junklet(let_name, name, separator) let(let_name) { "#{name}#{separator}#{junk}" } end