[Rspec-devel] Abstract contexts

Antti Tarvainen antti.tarvainen at iki.fi
Sat Jun 10 17:23:06 EDT 2006


> In this example, "should remove top when sent pop" in "A stack with
> one item" is essentially overriding "should remove top when sent pop"
> in "a stack that's not empty". In this framework that seems a likely
> source of confusion to me. What if we didn't allow this overriding? If
> there is truly common behaviour, it could be expressed in the
> abstract_context, but if it differs across concrete contexts, then it
> has to appear in each one?

Yes, I agree that this can be confusing. One possible solution is to
have a separate method to override specifications, such as

override_specification "should remove top when sent pop" do
  @stack.pop.should.be 10
  lambda { @stack.pop }.should.raise StackUnderflowError
end

> The other thing that concerns me is that the setup in "A stack with
> nine items" must use (2..10) because the specs in the abstract
> contexts have hard coded values. So we've got these bindings between
> the concrete and abstract contexts that could make it difficult to
> understand why something is failing.

This is also true. How about this:


def initial_stack_values(how_many=10)
  (11-how_many..10).to_a
end

abstract_context "A stack that's not empty" do
  specify "should return top when sent top" do
    @stack.top.should.be initial_stack_values.last
  end
# ...
end

context "A stack with nine items"
  is "a stack that's not empty", "a stack that's not full"

  setup do
    @stack = Stack.new
    initial_stack_values(9).each { |i| @stack.push i }
  end
end


Antti T.


More information about the Rspec-devel mailing list