From: "David A. Black" Date: 2008-08-03T04:17:13+09:00 Subject: Re: unit testing advice Hi -- On Sun, 3 Aug 2008, James Britt wrote: > David A. Black wrote: >> Hi -- >> >> On Sun, 3 Aug 2008, Shadowfirebird wrote: >> >>> I certainly "get" the idea that it's better to write the tests first. >>> Unfortunately my dinosaur brain needed to write some code to prove >>> that my class model was workable...! >> >> I often have to bootstrap myself into an application by getting >> something running before I can get my brain into test mode. Certainly >> with something like Rails, there's a lot to do before any tests are >> written, since you can't write unit tests before you know what your >> models are (and generating the model so conveniently writes the test >> file for you :-) > > Isn't the idea of TDD and BDD that you discover what classes and method are > needed by writing the tests first? So there *should* be no model in place > prior to writing the test; the initial failure of the test is what drive the > creation of the model. That may be the ideal for some people, but not for me when I'm modeling a domain and especially when I'm working out a database schema. I don't consider unit tests to have superseded the other tools that support those activities, including blank pieces of paper and index cards and so on. I guess you could write unit tests for a Rails app before creating the models, if you mocked up the objects' attributes, their associated objects, and so on (since there would presumably be no database yet), and then rename your files so they don't get clobbered when you generate the test files... but it seems like it would be terribly arduous, with no real gain, and I don't think I've ever seen anyone do it. >> One thing to remember is that TDD is about development, and that not >> all instances of entering code on a keyboard are development. And >> we've all, I believe, learned an absolute ton from exploration and >> experimentation that wasn't part of the process of ongoing application >> development. It's important not to feel like you're being >> unprofessional or sloppy if you happen to want to try out some code in >> a file, or in irb, and you don't write a test for it. >> >> In fact, one question that has come to intrigue me recently is the >> question of whether there are any active programmers who have >> literally written code test-first from the time they first learned how >> to program onward. I suspect the answer is no -- and if that's the >> case, it means that there is no evidence for the position that it's >> always, automatically bad to write code without a test. > > > Has anyone ever seen a Learn To Program or Learn Language Blub book that did > TDD? I doubt such a thing exists. Instead, people are shown code, > encouraged to write code, then (in so many words) told that what they were > shown and told is not the right way to code. On the other hand, having a > unit test for the 1-liner helloworld.rb seems massively goofy. The thing is, all the people who argue that testing first is the right way to code did not themselves learn to code that way. That doesn't prove or disprove anything, but it does make me wonder whether teaching someone test methodology right out of the starting gate is demonstrably the best way to go about teaching someone programming. I tend to think it isn't, though I also think that there are good and bad ways to introduce testing into the mix (the best way probably being to present it as essentially what they've been doing all along, but more structured; and the worst way being the "OK, the fun is over, now let's get serious" stuff). David -- Rails training from David A. Black and Ruby Power and Light: * Advancing With Rails August 18-21 Edison, NJ * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates!