Created
June 2, 2010 22:12
-
-
Save pcreux/423097 to your computer and use it in GitHub Desktop.
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
# Set @@variable_name in a before(:all) block and give access to it | |
# via let(:variable_name) | |
# | |
# Example: | |
# describe Transaction do | |
# set(:transaction) { Factory(:transaction) } | |
# | |
# it "should be in progress" do | |
# transaction.state.should == 'in_progress' | |
# end | |
# end | |
def set(variable_name, &block) | |
before(:all) do | |
self.class.send(:class_variable_set, "@@#{variable_name}".to_sym, instance_eval(&block)) | |
end | |
let(variable_name) do | |
self.class.send(:class_variable_get, "@@#{variable_name}".to_sym).tap do |i| | |
if i.respond_to?(:new_record?) | |
i.reload unless i.new_record? | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment