public
Created

  • Download Gist
rspec-set.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
require 'rspec'
 
def set(attribute, &block)
before(:all) do
instance_variable_set("@#{attribute}", yield)
end
 
let(attribute) { instance_variable_get("@#{attribute}") }
end
 
class Foo
attr_reader :bar
 
def initialize(i = 0)
@bar = i
end
 
def bar!
@bar += 1
end
end
 
describe :set do
 
set(:foo) { Foo.new }
let(:foo2) { Foo.new }
set(:foo3) { Foo.new(10) }
 
it "should have foo.bar == 0" do
foo.bar.should == 0
end
 
it "should have foo2.bar == 0" do
foo2.bar.should == 0
end
 
it "should have foo3.bar == 10" do
foo3.bar.should == 10
end
 
context "when incremented" do
before(:all) do
foo.bar!
foo2.bar!
foo3.bar!
end
 
it "should have foo.bar == 1" do
foo.bar.should == 1
end
 
it "should have foo2.bar == 0" do
foo2.bar.should == 1
end
 
it "should have foo3.bar == 11" do
foo3.bar.should == 11
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.