RSpec and the joy of testing 2

Posted by Matt on January 29, 2008

rspec89 database tables, 93 fully RESTed objects, 4740 successful tests. Life is grand.I’ve spent years working in web development with PHP (as I’ve describe ad naseum), and I’d hate to admit it, but I’ve never written a test. Ever. My stuff works, but soon after every launch, small cracks start to appear. The worst part is that these small cracks are always found by my users. This is not only embarrassing, but it leads to me furiously debugging a live system with active users, trying to quickly recreate the conditions that caused the error. I like my hair and I’ve losing it fast enough, thank you.Enter RSpec and Behavior Driven Development (BDD). RSpec makes writing tests a joy. Yes, I know, I know, but testing can be a real joy. It’s like working out; it always seems super difficult to start, and you stall and think of a billion other things to do, but once you actually start you feel really good and alive.BDD makes testing second nature, and RSpec makes writing these tests intuative. You know those meetings you have with your clients and you try to distill what they truly need from what they tell you they need? BDD gives you a language that both you and your clients can speak; you simply describe how an object should behave:A Car not only has a color, it also should not be driveable with one tire and it should only be driveable by a licensed driver.

describe "A Car" dobefore(:each) do@car = mock_model(Car)@driver = mock_model(Driver)endit "should have a color" do@car.color = nil@car.should_not be_validendit "should not be driveable with one tire" do@car.tires = 1@car.is_driveable?.should_not be_trueendit "should be driveable with four tires" do@car.tires = 4@car.is_driveable?.should be_trueendit "should not be driveable by an unlicensed driver" do@driver.stub!(:license_active?).and_return(false)@car.stub!(:driver).and_return(@driver)@car.is_driveable?.should be_falseendit "should be driveable by a licensed driver" do@driver.stub!(:license_active?).and_return(true)@car.stub!(:driver).and_return(@driver)@car.is_driveable?.should be_trueendend
See? Simple. As with most Ruby code, RSpec features English like statements that are immediately comprehensible to anyone—even clients. You know what the best thing is about RSpec though? It not only tests your code, it documents your code. Just look:
A Carshould have a colorshould not be driveable with one tireshould be driveable with four tiresshould not be driveable by an unlicensed drivershould be driveable by a licensed driver
Imagine testing and documenting your code at the same time! Two things that many programmers skip altogether! That alone makes the Ruby on Rails framework and mindset the most attractive choice today.

RSpec TextMate Bundle Tweak

Posted by Matt on January 21, 2008

One thing that annoys me when using the RSpec bundle for TextMate is the ‘it’ snippet. I think that any new spec should at least register as ‘Pending’ when it is run, not automatically pass. Having a blank test pass is really kind of dumb. So I’ve tweaked the ‘it’ snippet just a hair to automatically insert a ‘Pending’ statement into a newly created ‘it…do’ block. The description will be highlighted first, then the entire ‘pending’ statement so it will be easy to enter your data.

To use this snippet, just do CTRL + Command + Option + B in TextMate, go to the RSpec listing, and click on the ‘it’ snippet. Copy and paste the below text and you’re in business.

it "${0:should ${1:description}}" ${3:do
  ${2:pending("Needs to be written")}
end}

Strange Phenomenon in Columbus 1

Posted by Matt on January 19, 2008

Light
Alright. I’m not nuts. Seriously.

I live in Blacklick, Ohio; a small suburb of Columbus. On January 9th, 2008 we had a massive wind storm overnight. Wind gusts of 55MPH and such. Around 1:20am, I heard a noise that raised me right out of my bed. The sound was of electricity escaping, almost like a Jacob’s Ladder without the snap at the end. The electricity in my house was flickering, trying valiantly to stay on. Outside at this exact moment, was a very bright greenish/blue light; it was so bright that it flooded my bedroom. We have light-blocking shades on our windows and it was still impressive.

This light almost looked as if someone was shinging a light down at you when you’re underwater, and it only appeared when the electricity was flickering. It appeared about two or three separate times. My wife and I were kind of freaking out as we thought that lightening was hitting our house. We quickly realized that there was no thunder and that the light was directly in sync with the electricity sound.

I ran to a window and was looking out at the light, thinking that a transformer must be blowing up (it was seriously that bright and loud). I realized that our neighborhood has no above ground electricity poles. None.

I grabbed a flashlight and looked out to our electicity box in our backyard and noticed that it wasn’t blackened, and still in one piece.

The power was out for a few more hours and I finally fell asleep around 3:00am. When I wake up in the morning, all is calm and the power is back on. Nothing was damaged at all.

So what was it? I have no fucking idea. I am an educated man, and I was completely sober. My wife saw the light and heard the sounds along with me.

Fast forward to today. My Father-In-Law calls and says that he was listening to a local radio program and they mentioned this phenomena. I guess that twenty or so people reported it, and an airline pilot saw it as he was approaching Columbus’ airport (CMH). They said that this exact phenomena was spotted by people in Texas around the same time.

I’m dying to know what this phenomena was. I’ve never seen anything like it. My only guess is that it was St. Elmo’s Fire, but I’m not sure what circumstances are required to create it.

Columbus mayor loves the funk 1

Posted by Matt on January 19, 2008

colemanLast year, my wife and I went to see Parliment Funkadelic at a local venue. Much to our surprise, the Columbus Mayor, Michael Coleman was in attendance! He actually introduced the band and said what an honor it was to talk with George Clinton backstage. Later on, I noticed the Mayor in the balcony grooving out to the sounds of the Mothership. I was pretty damned impressed that the Mayor of a major metropolis knows how to get down, because man, if you can get down with PFunk, you know what’s up.

Fast forward to today whilst eating breakfast downtown I ran into Mayor Coleman. We just happened to be standing in line next to each other. He was smiling and saying hello to others when I asked him if he was going to see PFunk in February when the stop at the OSU campus. His expression changed immediately, he looked over to me and said ‘When are they coming to town?’. He said that he would definately try to make it. He pulled out his Treo like he was going to make a note of it. He told me and my wife that he has a picture of Bill Clinton and George Clinton hanging in his office!

It’s funny that people were taking pictures of him and shaking hands and I just ask if he’s going to PFunk like one of my buddies. A little personal moment with the Mayor ;)

MacWorld 2008 1

Posted by Matt on January 15, 2008

So the MacBook Air was finally unveiled and wow, what a stunner. I’m sure you’ll read the specs and see the pictures a billion and a half times today, so I’ll spare you. As usual MacWorld showcased some brilliant items, but what about the MacBook?

I dig the MBA, but I don’t need an ultraportable; especially one at that price. The MacBook strikes a great balance between affordability and Mac goodness, but where are the updates? No aluminum? No faster processor? No price fluctuation?

I only complain as I was going to give my beloved MacBook to my wife after I bought an updated one after MacWorld. With the MBA clocking in at $1800 and no bump in love for the MacBook, what am I to do? I don’t necessarily want to purchase another exact copy of this MacBook because hey, that’s just not fun.

I guess this is a good problem to have ;)

What Does Ruby On Rails Feel Like? 2

Posted by Matt on January 11, 2008

Some programmers talk about ‘code smell’, or ‘code feel’; what about a framework? What does RoR ‘feel’ like? I’ve been talking (blabbering) about Ruby on Rails to anyone that will listen or has the ill fortune to be within earshot of me for the last month. Usually this person is my beloved wife. At this point I’m sure if I mention Rails once more to her I’ll be sleeping outside. Anyhow, during one of my rants she stopped and asked me “What’s the difference?”. To her, a programming environment is obviously alien, but to a programmer it’s our world. Our day is spent in that world, and most of our creative output is expressed in that world’s tongue.

Before I pulled my head out of my nether regions (gave up and started using frameworks), I coded everything by hand in PHP. Then one magical day I sat down and decided to see what this CakePHP thing was all about. After realizing that I was doing far too much work designing the infrastructure that a framework provides on top everything else, I immediately switched. CakePHP cut my work by 80%. Seriously. It’s a godsend for PHP people who haven’t yet realized that Ruby On Rails is the path to eternal salvation.

I hacked happily away with CakePHP for six months or so; did a project or three. Very soon I realized that hacking is fun and all, but I was really feeling the limitations of PHP as a language itself. PHP gets the job done, but in an extremely non-elegant way. This moment coincided with the release of Rails 2.0. In a fit of coincidental serendipity, I decided to give Rails another look now that I’d decided that frameworks were a Good Thing.

A shade over a month later, I’ve read six Ruby/Rails books cover to cover. I’ve ported existing PHP development to RoR. I’ve dropped PHP permanently. It is sincerely that good. It’s career altering.

So what does RoR ‘feel’ like? It feels like cheating. It feels like starting a marathon and then taking a taxi to the finish line. Remember the 80% workload reduction I achieved by jumping into a framework? With RoR I would honestly say that I’m coding at 5% of my original workload. Much like the 1950’s promise that technology would slow down our lives, the actual consequence is that I have more time to do better work. I’m not just filling 5% of my development time, I’ve freed up 95% to conquer other aspects of coding, such as Behavior Driven Development, requirements analysis, and web design.

RoR is like cheating. It’s like connecting dots. When done right it’s the perfect zen-like hum between both halves of your brain. Yes; it’s that good.

2008

Posted by Matt on January 01, 2008

Imagine there’s no Heaven
It’s easy if you try
No hell below us
Above us only sky
Imagine all the people
Living for today

Imagine there’s no countries
It isn’t hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

You may say I’m a dreamer
But I’m not the only one
[Imagine lyrics on http://www.metrolyrics.com]

I hope someday you’ll join us
And the world will be as one

Imagine no possessions
I wonder if you can
No need for greed or hunger
A brotherhood of man
Imagine all the people
Sharing all the world

You may say I’m
a dreamer
But I’m not the only one
I hope someday you’ll join us
And the world will live as one