lucaspiller/espec — pull request #18 by benmmurphy (2011-12-27 22:19:18+00:00)
Title: add rspec like instance variables

partially closes #17.

I wrote an rspec ruby test to check if it acts similar to the spec I made. Here is the code:

require 'rspec'

def increment(atom)
puts "#{atom}, #{@foo.inspect}"
@foo = @foo.nil? ? 0 : @foo + 1
end

describe "foo" do
before :all do
increment(:before_all)
end

before :each do
increment(:before_each)
end

it "should do stuff" do
increment(:example)
end

describe "nested stuff" do
before :all do
increment(:nested_before_all)
end

before :each do
increment(:nested_before_each)
end

it "should do nested stuff" do
increment(:nested_example)
end

after :each do
increment(:nested_after_each)
end

after :all do
increment(:nested_after_all)
end
end

it "should do more stuff" do
increment(:example2)
end

after :each do
increment(:after_each)
end

after :all do
increment(:after_all)
end
end

This is the output of the two programs. They are slightly different because rspec runs all examples on the current level before running nested examples. We should think about doing this.

before_all, nil
before_each, 0
example, 1
after_each, 2
should do stuff
before_each, 0
example2, 1
after_each, 2
should do more stuff
nested stuff
nested_before_all, 0
before_each, 1
nested_before_each, 2
nested_example, 3
nested_after_each, 4
after_each, 5
should do nested stuff
nested_after_all, 1
after_all, 0

Finished in 0.38413 seconds
3 examples, 0 failures

[{before_all, undefined},¬
{before_each, 0},¬
{example, 1},¬
{after_each, 2},¬
{nested_before_all, 0},¬
{before_each, 1},¬
{nested_before_each, 2},¬
{nested_example, 3},¬
{nested_after_each, 4},¬
{after_each, 5},¬
{nested_after_all, 1},¬
{before_each, 0},¬
{example2, 1},¬
{after_each, 2},¬
{after_all, 0}]