This post originated from an RSS feed registered with Ruby Buzz
by Jason Watkins.
Original Post: a more declarative rspec?
Feed Title: Jason Watkins
Feed URL: http://jasonwatkins.net/rss
Feed Description: blog.collect{ Thought.rand }
I’ve been intrigued with rspec since seeing the steady trickle of blog posts from folks like obie
Looking at this example some thoughts occurred to me.
How can I easily see what context a given behavior applies to?
If I want to understand a specific behavior, how do I know which places to look in the spec without scanning the whole file?
How could we make this more DRY so that there’s less temptation to copy-past behaviors between contexts?
So I decided to riff a quick example of a couple ideas:
rake like dependent contexts
behaviors apply to context groups
require'a_more_declarative_rspec'context"empty"do@stack=Stack.new:limit=>10endcontext"one item"=>"empty"do@stack.push10endcontext"almost full"=>"empty"do(2..10).each{|i|@stack.pushi}endcontext"full"=>"empty"do(1..10).each{|i|@stack.pushi}endcontext_group"not empty"=>["one item","almost full","full"]context_group"not full"=>["empty","one item","almost full"]behavior"should accept an item when sent push"=>"not full"dolambda{@stack.pushObject.new}.should.not.raiseendbehavior"should complain when sent top"=>"empty"dolambda{@stack.top}.should.raiseStackUnderflowErrorendbehavior"should complain when sent pop"=>"empty"dolambda{@stack.pop}.should.raiseStackUnderflowErrorendbehavior"should return top when sent top"=>"not empty"do@stack.top.should.be10endbehavior"should not remove top when sent top"=>"not empty"do@stack.top.should.be10@stack.top.should.be10endbehavior"should return top when sent pop"=>"not empty"do@stack.pop.should.be10endbehavior"should remove top when sent pop"=>"not empty"do@stack.pop.should.be10contexts"almost full","full"do@stack.top.should.be9endcontexts"one item"dolambda{@stack.top}.should.raiseStackUnderflowErrorendendbehavior"should remove top when sent pop"=>"one item"do@stack.pop.should.be10endbehavior"should complain on push"=>"full"dolambda{@stack.pushObject.new}.should.raiseStackOverflowErrorend
What I like:
one place to look for a given behavior
more dry
should reduce bloat when we’re forced to deal with things that have very wrinkly behavior
What I dislike:
it’s far more cluttered and complex than the original
it can require some creativity to structure contexts such that an expectation can apply verbatim across them all (example: make sure 10 is top of all the non empty stacks)
What do you think? A step in a promising direction, or simply a step to far?