Skip to content

Instantly share code, notes, and snippets.

@pcreux
Created May 26, 2010 05:55
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 pcreux/414120 to your computer and use it in GitHub Desktop.
Save pcreux/414120 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment