GSpec for BDD

Look at that title! I’m off and running again with some new buzz words that I just learned twenty minutes ago. I’m not even sure if I know what I’m talking about but since I love talking about things that I don’t know I’ll continue rambling here. I was perusing the net and I happened across a Google talk on BDD, or Behaviour Driven Development. The talk really scratched that itch I’ve been having. Everytime I try to explain TDD to someone I stumble over the same confusing terms, “blah blah, test… blah, blahuse case. Rather than ramble I wanna sitck something right in the middle of the page here that says it clearly.

We want to think about how clients can use the object, not how the object will go about satisfying its clients’ needs.

Gosh that feels on point. I think I wanna hang that saying up on my wall at home. I wanna put it on my license plate but the stoopid DMV limits you to seven characters. I got so carried away by the Google talk that I immediately started dreaming of what the code would look like in Groovy. My day dream dug up the name GSpec. The whole big idea involves authoring executable specifications for your app. Even better explained, they would be examples.

Here’s a groovy example:

context("A new stack") {
   setup {
      stack = new MyStack()
      stack.setMetaClass(new GSpecDelegate(stack.getMetaClass()))
   specify("should be empty") {

I’m augmenting the above script with my ugly initial hack at RSpec in groovy. Here’s what I came up with after an hour of coding:

class GSpecDelegate extends DelegatingMetaClass {
   GSpecDelegate(MetaClass meta) { super(meta) }

   Object getProperty(Object obj, String name) {
      if(name.startsWith('should_be_')) {
	 def prop = delegate.getProperty(obj, name.substring(10))
	 assert true == prop : name
	 return prop
      else return delegate.getProperty(obj, name)

def invokeMethod(String name, def args) {
   switch(name) {
      case "context":
         println args[0]
      case "setup":
      case "specify":
         println "\t" << args[0]

Don’t laugh because I know it’s perfectly obvoius that I don’t know a thing about Groovy, RSpec or Ruby. (I’m just making it all up as I go along.) That crude example was enough to get me through the first page of the Stack example from RSpec. What happens here is an error is thrown because we can’t resolve class MyStack. So now I add the following:

class MyStack {
	def empty = false

Now I get the following in my console:
A new stack
should be empty
Exception thrown: java.lang.AssertionError: should_be_empty. Expression: (true == prop). Values: prop = false

I gotta say I love the idea. I wish I had more time to blog about it but responsiblity calls and I have to sign off for now. To all my ladies, all my fellas, all my peoples, drop it like it’s hot in the space where it’s not…

(In case you’re confused by the above signoff the place on this page that is of relatively cool temperature would be the comment box. Filling it with your ideas would add fuel or heat to the fire that sparked the above post.)

7 thoughts on “GSpec for BDD

  1. That’s a nice framework. I watched about 3/4 through the video and jumped into the site to browse a bit. There are a lot of ideas that could be incorporated into a hypothetical Groovy version of RSpec, bringing the goodness from all projects into one powerful tool for Java developers. Imagine a technology that you could give to Java developers that would allow them to write functional specs as well as lower level detailed API specs for their code. The specs would read like english and run directly on a JVM against their compiled codebase. That would be off the chains! Really though, I’m just planting seeds for a new idea. It probably would require a more in depth understanding of each project before anything useful would come from it.

  2. GSpec so far is a jar file sitting on my harddrive and does little more than what I’ve written about here. I plan to use it on my next “work” project where I’ll no doubt enhance it as I use it. With that I have a bit of a backlog on some of my “work” projects and I’ve been trying to complete a demo program for career day at my daughter’s school. I’ll need about another week or two before I can clear my plate enough to play with it. Maybe in the interim I’ll upload what I have to some temporary storage and link to it for people like yourself that are interested.

  3. Hmm. You could go so far as to spawn an OSS project for it. Maybe encourage lots of different partial implementations to explore the various demands on a gSpec?

    I’m not suggesting that this has to be a clone of rSpec. It could follow it’s own Groovy-natural direction also.

  4. Honestly, I’ve been thinking just that myself. Everything you’ve said (especially the “not cloning but following it’s own direction” part) is exactly what I’ve been thinking. It has a lot of potential which is why I wanted to float it on my site to generate some interest. I plan to post another article about it and submit to digg to spark some curiosity. Then I’d probably start an open source project. Then I’d sell the rights to Sun microsystems, write a book, sell the right to the book to MGM studios, buy a mansion in Tahiti and play on my PlayStation 720 all day. Give me a minute to save my rear end from termination though. I’m still backed up on my normal job from all of the side stuff I’ve been doing and posting. If your inteerested or have any info on how to get such a project going I don’t mind involving you.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s