CATEGORY: Ruby On Rails

Testing with Ruby On Rails

WED, 22 MAR 2006

Remember those days when you fixed a bug only to introduce more bugs in return? That’s what testing is all about, to be more specific is incremental testing. When you write some procudures, you should also write the test procedures. So when a procedure is modified, you retest all the procedures available, then your codes will be less breakable.

Ruby On Rails (RoR) is built with incremental testing in mind, it supports unit testing, functional testing, mock object, performance testing, benchmarking and test data loading support. In java you will have to look at different components such as JUnit, Canoo WebTest, JMock/Easy Mock, JMeter, and DBUnit, not to mention many other libraries trying to do similar things. The problem is the learning curve is high and the components are not as cohesive as RoR test framework.

Here are some points that I like most about RoR test framework compared to Java framework:

Test Templates
It supports auto creation of test templates for controller and model class.
Test Data Format:
Thank God it’s not xml, it’s called YAML (YML Ain’t XML) or “fixtures” the term used in RoR. It’s very easy and intuitive, and hell ya, you can even include ruby codes in there, e.g.
user_supervisor:
        id:             supervisor
        name:           Clark Kent
        date_available: <%= 1.day.from_now.strftime("%Y-%m-%d %H:%M:%S") %>

<% for i in 1..20 %>
user_<%= i %>:
        id:             <%= i %>
        name:           User <%= i %>
<% end %>
Unit Test
In your test code, you can refer to the test data using @user_supervisor, which is very important to create a more flexible test code that caters to changes in test data without breaking your test procedures. e.g.
test_load_supervisor:
        ....
        assert_equal user.name, @user_supervisor.name
end
Rake Command
Rake command is Ant’s brother in Java, they do similar things, only Rake has built-in useful options for developer. I love “rake recent” to test recent updated unit tests (updated in last 10 mintues).
Fewer Lines of Codes
I just can’t resist repeating this point!!!

Ruby On Rails Maybe God Sent

MON, 20 MAR 2006

Last weekend I decided to get my hands dirty on Ruy On Rails. I bought Agile Web Development with Rails book and got hooked immediately.

I studied Visual Basic during my Uni days, but fell in love with Java after my first job. And I’ve been faithful every since, Java has tought me the beauty of object oriented programming, design patterns and frameworks. But due to its flexibility and large number of open source projects. It prones to entangle developers into a mesh best practices to adopt.

For web framework itself, it ranges from request-based (Struts, Web Work, Spring MVC) to component based frameworks (JSF, Tapestry, Wicket), and the combination of both (Stripes) to full-stack framework like RIFE and Appfuse. I used Struts before and when I was looking for a better framework I have to read up JSF, Tapestry and Spring MVC. And I finally settled with Spring MVC, though each framework have its own good and bad points. How about persistence framework (Hibernate, EJB, JDO, iBATIS)?

Learning Ruby On Rails is a different experience, it felt  like sailing through a calm sea with warm sun light and breezy wind. Maybe my bakground in Java and web development industry helps. But it is also mainly due to Ruby’s main guiding principles,  “Don’t Repeat Yourself” and “Convention Over Configuration.” So things are laid for you, chosen and decided. No questions asked. The codes are short, clean, and there are lots of shortcuts. It may look weird at first, but you’ll get used to it.

Compared to Java, Ruby is still very young, but being young, it is able to learn from the mistakes made by its predecessors Java and other languages. And coming from different perspectives to improve developers productivity, and with higher customer expectation each day for each dollar spent, especially in Asia, Ruby may be God sent. I’ve yet to further explore Ruby and to complete the other half of the book; there could be some quirks, but I hope there won’t be many.