Skip to content

@pcreux /rspec-set.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.