Rails and TDD

I’m trying out the whole TDD on my newest project, and here’s my
dilemma:

I am creating a login system (I know, I know… re-inventing the wheel,
blah blah blah; I’m doing this to teach myself things, not because I
don’t think there’s something out there that can handle what I want),
and so far, I’ve realized I’m going to need a User model and a User
controller. I’ve created both of them, and I’m starting to write the
tests that I will be developing towards passing: my question is, should
I write tests for the controller, then the model; the model, then the
controller; or try to do them both in tandem? I’m having problems
figuring out where to start with this, as I’ve never done TDD before and
I’m fairly new to the Rails game in general.

Do the model first, since the controller will depend on the model.


Building an e-commerce site with Rails?
http://www.agilewebdevelopment.com/rails-ecommerce

Meet up at RailsConf:
http://railsconf2007.conferencemeetup.com/

I did this exact same exercise when I was learning rails and it’s a
GREAT way to pick up both TDD and Rails because down the line you’ll
be able to compare your results against some of the login generators
out there and see where you and the others differ.

It’ll also give you a thorough understanding of your login system
which sadly, if generated, can be a hole in someone’s intellectual
command of one of the most crucial pieces of their application.

For learning TDD I suggest checking out Geoffrey Grosenbach’s peep cast
on it:
http://peepcode.com/products/test-first-development

Oh and also if you want a headstart on the login system Chad F.'s
Rails Recipes has some nuggets for both authentication and
authorization.

I haven’t viewed this particular peepcast but I’ve watched the others
and they are excellent.

Good luck,

Tim C.
[email protected]

On 1/18/07, Luke I. [email protected] wrote:

I’m trying out the whole TDD on my newest project, and here’s my
dilemma:

I am creating a login system

Break this feature up into tasks, each representing one user
interaction. Example:

  • logout
  • login bad user name
  • log in bad password
  • log in first time

For each one, write enough Railage to enable only that interaction.
Each one may require tweaks to a database, model, controller, and/or
view.

Write test cases before as many tweaks as possible. Pass all tests
after every few edits. If you can’t think of a test (look up
assert_select and submit_form to test the view), then at least add a
test after you write the code

(I know, I know… re-inventing the wheel,

Hardly. Many erudite, educated, senior, overpaid, world-class
programmers still don’t TDD, and anyone who does will easily blow them
away…


Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!

Definitely test the model first. Check out Rcov as well. It’s a great
way to help make sure your tests are complete. It won’t make sure
you’re testing every possible scenario, but it’s a great start.