The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
a more declarative rspec?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jason Watkins

Posts: 51
Nickname: jasonw
Registered: Oct, 2003

Jason Watkins is a rails developer with PLANET ARGON in the portland metro area.
a more declarative rspec? Posted: Apr 25, 2006 1:26 PM
Reply to this message Reply

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 }
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jason Watkins
Latest Posts From Jason Watkins

Advertisement

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 => 10
end

context "one item" => "empty" do
  @stack.push 10
end

context "almost full" => "empty" do
  (2..10).each{ |i| @stack.push i }
end

context "full" => "empty" do
  (1..10).each{ |i| @stack.push i }
end

context_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" do
  lambda{ @stack.push Object.new }.should.not.raise
end

behavior "should complain when sent top" => "empty" do
  lambda{ @stack.top }.should.raise StackUnderflowError
end

behavior "should complain when sent pop" => "empty" do
  lambda{ @stack.pop }.should.raise StackUnderflowError
end

behavior "should return top when sent top" => "not empty" do
  @stack.top.should.be 10
end

behavior "should not remove top when sent top" => "not empty" do
  @stack.top.should.be 10
  @stack.top.should.be 10
end

behavior "should return top when sent pop" => "not empty" do
  @stack.pop.should.be 10
end

behavior "should remove top when sent pop" => "not empty" do
  @stack.pop.should.be 10
  contexts "almost full", "full" do
    @stack.top.should.be 9
  end
  contexts "one item" do
    lambda{ @stack.top }.should.raise StackUnderflowError
  end
end

behavior "should remove top when sent pop" => "one item" do
  @stack.pop.should.be 10
end

behavior "should complain on push" => "full" do
  lambda{ @stack.push Object.new }.should.raise StackOverflowError
end
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?

Read: a more declarative rspec?

Topic: DRb an Introduction and Overview Previous Topic   Next Topic Topic: You Are Born With No Body Parts...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use