If you can start mastering the concepts of Test Driven Development, you
would be doing yourself a huge favor. This is what I do:
- Create a scaffold_resource for the functionality and model I want to
build (I am really into RESTful development these days)
- Write an integration test that contains its own Domain Specific
Language (or DSL) so I can write my test cases in english. I basically
write out a user story of what I want this part of the app to do. I’ll
put an example after this list.
- Run my integration test and watch it fail in a blaze of glory.
- Write the code for my migration and model to provide the model-based
functionality needed for the integration test.
- Run the unit tests built by the scaffold, then work the code in the
model until the unit tests pass.
- Write the controller code required to make my integration tests
pass. I usually do these one at a time (my integration tests have a
number of small scenarios to test in them). Some of the tests will
usually pass with the stuff the scaffold generates. That is considered
a bonus! I work the rest one at a time, running the test in between
each iteration, until everything works.
Now if your integration test is a good representation of the user story
for that functionality, when you get to this point, your code should be
fairly complete. All you should have to do is get your views in order
and you’re golden.
Here is an example integration test that I have used:
class ForumTest < ActionController::IntegrationTest
fixtures :forums, :forum_posts, :users
quentin = enter_site(:quentin)
forum = quentin.selects_forum
quentin.logs_in(“quentin”, “test”, forum)
q_post = quentin.posts_to_forum forum
aaron = enter_site(:aaron)
posts = aaron.searches_forums
a_post = aaron.selects_post_from_results q_post
a_reply = aaron.replies_to_post(forum, a_post)
reply = quentin.notes_new_reply q_post
def logs_in(user_name, password, forum=nil)
# you get the picture
def is_reply_aarons_post?(reply, d_post)
open_session do |session|
session.name = name
yield session if block_given?
There is a ton of info on TDD out there, so you should have no trouble
getting really good advice on the subject.
Hope this helps!