tags:

views:

38

answers:

1

If a spec file contains this before the it() example groups, what does it mean?

context "when almost full (with one element less than capacity)" do
  before(:each) do
    @stack = Stack.new
    (1..9).each { |n| @stack.push n }
  end
end

context "when full" do
  before(:each) do
    @stack = Stack.new
    (1..10).each { |n| @stack.push n }
  end
end

Which one will be the one that is executed before?

I don't get it.

+1  A: 

before(:each) will get run prior to running any specs that follow. So for example, in your spec for a full stack, any specifications will have a full stack set up prior being executed. You don't have any It methods, so that does not really occur at present however.

It may be worth noting there is before(:all) which will be run once, prior all specs for that context. Whereas before(:each) gets run prior to each spec.

Finglas
but i have two different before(:each) in the example. i wonder which one will be executed if i have had some it() blocks.
never_had_a_name
The `before(:each)` method is run for all examples within the context where it is defined. The way your spec is written the two `before(:each)` methods are in separate contexts, so they will both run, but not for the same examples.
zetetic
Zetetic beat me to my reply, but he's spot on. As the contexts are not nested, each set up will be run individually, and not interfere with each other.
Finglas