Skip to content

Instantly share code, notes, and snippets.

@pcreux
Created June 2, 2010 22:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcreux/423097 to your computer and use it in GitHub Desktop.
Save pcreux/423097 to your computer and use it in GitHub Desktop.
# 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