Irony
Irony
Submitted by Brian Marick's blog on Fri, 16/06/2006 - 14:00.I'm practicing for a set of five demos I'm doing next week. In each, I'll work through a story all the way from the instant the product director first talks about it, through TDDing the code into existence, and into a bit of exploratory testing of the results. Something interesting happened just now.
Step one of the coding was to add some business logic to make a column in a Fit table pass.
In step two, I worked on two wireframe tests that describe how the sidebar changes. These tests mock out the application layer that sits between the presentation layer and the business logic.
What remained was to change the real application layer so that it uses the new business logic. That, I said (imagining my talk), is so simple that I'm not going to write a unit test for it. Even if I do mess it up (I claimed), I have end-to-end tests that will exercise the app from the servlets down to the database, so those would catch any problem.
You can guess the results. I made the change and ran the whole suite. It passed. Then I started up the app to see if it really worked, and it didn't. The problem is in this teensy bit of untested code:
def may_add_user?
Model::Permissions.new.may_add_user?(@current_sesssion)
end
The problem is that I have an extra sin . In Ruby, a previously unmentioned instance variable has value nil. It happens that, to the business logic, nil means "no session, so not logged in, so not allowed to create a user."
From this, we can draw two lessons:
Maybe all those people who say that even code within a class should go through accessors to get to instance variables are right. Had I done that, the program would have failed—in the end-to-end tests—with a "no such method" error.
Hey, one-time program chair of the Pattern Languages of Programs conference, there's this pattern called Null Object...
I'm still not inclined to write a unit test.
This is the neatest thing to happen to me today. But nothing like it better happen in the real demo.
