Skip to content

Instantly share code, notes, and snippets.

@reddyonrails
Last active May 12, 2017 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reddyonrails/d321785d195a6cecb8f02842afc69db4 to your computer and use it in GitHub Desktop.
Save reddyonrails/d321785d195a6cecb8f02842afc69db4 to your computer and use it in GitHub Desktop.
Rspec custom macro like let or let! => pass multiple keys to define array of lets under example group
module RSpec
module Core
module MemoizedHelpers
module ClassMethods
# Usage: ways of defining config getters
# Before:
# let!(:user_1) { DataSource.get(:user_1) }
# let!(:user_2) { DataSource.get(:user_2) }
# After:
# keys = [:user_1, :user_2]
# let_config_list!(keys)
def let_config_list!(keys, &block)
unless block_given?
block = ->(key) { OtherSource.get(key) }
end
running_example= caller[0]
keys.each do |key|
MemoizedHelpers.module_for(self).send(:define_method, key, -> {
running_example.instance_variable_get("@#{key}") ||
running_example.instance_variable_set("@#{key}", block.call(key))
})
before(:all) { __send__(key) }
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment