Last active
May 12, 2017 05:12
-
-
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
This file contains 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 characters
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