Wednesday, August 08, 2007 -
A lot people keep emailing me and letting me know how much they like the Rails webcasts I've been doing so I thought I'd continue today with a webcast all about their Unit Testing framework.
I mention this in the video, but it's worth writing here: I'm not trying to "convert" or sway people - my goal is to show what's out there. Rails has some very very cool stuff that goes into it, and it's built-in unit testing feature is quite cool. Maybe one day we can see something like this in the .NET stack that's part of the framework and not a function of the IDEĀ (no EULA comments please :):):).
This webcast is about 20 minutes long and I show you how to set up Unit Tests, and also some tips and tricks for generating test data using YML.
For example, suppose I want to use a dropdown to call a controller action on select. How would I do that?
In terms of the UI stuff - it's actually very easy. For most things, there's a Rails short cut - for instance, to setup a link to a different page you write:
<%=link_to "link text", :controller => "mycontroller", :action => "myaction"%>
In your case, you might want to wrap your dropdown in a form, and then post the results to a controller. That's pretty simple:
(the form code)
<% form_for :myobject, :url => { :action => :myobject_controller_action } do |form| %>
#now you have a form that is "dedicated" to a model and controller/action set. To create a drop down you can:
<%= collection_select(:myobject, :myobject_foreign_key ,LookupContoller.find(:all), :id, :name) %>
This will load up your dropdown for you.
One thing I needed to get used to was using more than one form - ASP only allows one per page - Rails uses them liberally :).
Keep in mind here that the Unit Tests are actually testing your web site - NOT just code behind. It's all the same to the tests which is a Major Leap if you ask me.
Also, forcing the separation of model and functional testing is brilliant :).