[rspec-users] RSpec makes me want to write better code
dchelimsky at gmail.com
Tue Mar 31 13:59:23 EDT 2009
On Tue, Mar 31, 2009 at 4:39 AM, Fernando Perez <lists at ruby-forum.com> wrote:
> Fernando Perez wrote:
>> Today is a big day. I officially transitioned from manually testing by
>> clicking around in my app, to automated testing with RSpec + Autotest.
> 6 months since my initial post, what happened in between?
> - My controllers are getting anorexic, and that's good. An action
> typically does only 1 call to a model and behind the scenes that model
> will make other calls to other models and do some fancy stuff, this used
> to happen in the controller which was bad. Now it has become so easy to
> write model specs.
> - I don't spec controllers, because it's too painful to do. I don't spec
> views either, because I tweak them too often, and it would take me more
> time rewriting view specs then actually coding the views. If there is
> something to test for is the rendering of the css across browsers,
> because that's terrible!
> - I use cucumber+webrat to test that some important public pages render
> without a 500 error. I don't care if the view has such div except if it
> is a critical one. What I mean is that I won't test, all the assignments
> that should be on the page, as some tutorials demonstrate. This is
> nearly impossible to maintain.
> - I can refactor my code, immediately spot areas where my code got
> broken, and fix it. Before some parts of my app would be broken without
> having me knowing about it for a long time until some cool customers
> report a few bugs, but this was not an acceptable situation.
> - I don't use Autotest, it sucks too much power, and it is too much
> distracting. From time to time I run specs to check if things look good,
> and always before updating the code on the server.
> - I have written my own Factory. It's not OOP, it could be refactored,
> but it works 100% as advertised, provides default values, can handle
> associations, etc. I am pleased with it (I tried factory girl,
> machinist, etc and got pissed off). I encourage anyone to write his own
> little factory, to get a better hang of how to write good specs. I
> totally got mad with fixtures, it is impossible to maintain specs/tests
> with fixtures. Impossible.
> - I don't do true BDD, i.e: I still write code before specs, because
> that's what motivates me. I consider that seeing my app living and
> writing code for my app more important than writing specs. Specs are
> still important, but only as a bug reporting tool, they don't add any
> value for the customer.
Please be careful when making absolute statements like this. First of
all, even "just a bug reporting tool" adds tremendous value for the
customer, because your catching bugs before they make it to
Secondly, the value to your customer of writing specs before code
depends on your personal skill level as it relates to writing them.
Spec-first adds considerable value for my customers because I know how
to move quickly through the red/green/refactor cycle, and it does
impact design, and therefore the quality of my code, and therefore the
short and long term maintenance costs. But, admittedly, I've been on
the TDD train ever since I learned about it back in 2003 and I kinda
know what I'm doing.
> In this crisis If I wanted an employee to resign
> by himself without paying him benefits (that's how it works in Europe),
> I would make him write specs all day long, and forbid him from seeing
> the app and play with it. He wouldn't last 1 week doing this.
Nor would anybody. From what you're writing, it sounds like your
perception is that you write a bunch of specs with no code, and then
write code to pass the specs.
The point of TDD is writing small examples and small bits of code in a
cycle. The point of BDD is to write high level scenarios so you know
what code to write, but then drive it out in detail with TDD. My
personal experience is that writing this way is a lot of fun, filled
with small successes every few minutes. Admittedly, it's not always as
fun as hacking together a quick prototype and seeing it come to life.
But the fun of that approach dies very quickly if the prototype
becomes the actual app and I find myself back-filling specs.
Hope that helps clarify.
> What about you?
> Best regards,
> Posted via http://www.ruby-forum.com/.
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users