h2. A Simple Stack - Moving specs between contexts So we now have the following spec.
$ spec stack_spec.rb -f s

A new stack
- should be empty

An empty stack
- should keep its mouth shut when you send it 'push'
- should not be empty after 'push'

A stack with one item
- should return top when you send it 'top'
- should not be empty after 'top'

Finished in 0.000882 seconds

3 contexts, 5 specifications, 0 failures
One thing that's odd about this is that "An empty stack should not be empty after 'push'". If you think about it, "An empty stack" is no longer empty after sending it 'push'. It's now "A stack with one item". Conveniently, we already have a context that expresses that. So it may make more sense to move that spec to the "one item" context. Remove it from "An empty stack" ... context "An empty stack" do specify "should not be empty after 'push'" do @stack.push 37 @stack.should_not_be_empty end end ... add it to "A stack with one item" ... context "A stack with one item" do setup do @stack = Stack.new @stack.push "one item" end specify "should not be empty after 'push'" do @stack.push 37 @stack.should_not_be_empty end ... end ... and adjust so it works better in this context. We're already pushing "one item", so we don't have to push 37. And we can adjust the name as well: context "A stack with one item" do setup do @stack = Stack.new @stack.push "one item" end specify "should not be empty" do @stack.should_not_be_empty end ... end
$ spec stack_spec.rb -f s

A new stack
- should be empty

An empty stack
- should keep its mouth shut when you send it 'push'

A stack with one item
- should not be empty
- should return top when you send it 'top'
- should not be empty after 'top'

Finished in 0.000838 seconds

3 contexts, 5 specifications, 0 failures
So keep your eye out for misplaced specifications like this. Feel free to move things around - carefully, of course. It may seem trivial at this point, but as your spec grows it's going to become more and more important. These specifications are documentation of your system. They must be clear, well organized, and readable. Previous