// Internet Duct Tape

Getting Started with Ruby on Rails – Week 3 – Testing

Posted in Ruby on Rails, Technology by engtech on December 05, 2007

Learning Ruby

I’ve fallen for the hype and started using Ruby on Rails for building database driven web applications. You can follow along with my weekly experience discovering gotchas with Ruby on Rails.

Previously: Getting Started With Ruby on Rails – Week 2

(I swear, back to your regularly scheduled non-rails content soon enough)

Gotcha #1 – after_initialized is after_instantiated

Yes, after_initialized is called more often than just when you call Model.new. Use if new_record? inside of it.

Gotcha #2 – button_to has it’s own class

You can’t pass :class parameters to the button_to helper because it creates it’s own :class=>”button_to”. Use :id instead.

Use console

script/console will give you an interactive console for playing with objects. Use it! It makes debugging tiny little gotchas with ruby syntax you might not be familiar with so much easier. Type reload! in the console to reload your models after any changes you’ve made. Type object.methods to see a list of everything an object responds to.

You can use many familiar console navigation keys like Up, Down to move between previous commands and Ctrl-A for start of line and Ctrl-E for end of line.

The Migration Shuffle

When you’re building a new migration on your development database always do the following:

rake db:migrate
rake db:migrate VERSION={current version - 1}
rake db:migrate

It’ll let you know that you’ve made an error in your down method right away, instead of weeks later when you’re trying to rebuild the database.

Little Bobby Tables

At some point your going to write a bad migration and screw up your development database, so rebuild it.

> mysql -u root -p
drop database proj_development; drop database proj_test;
create database proj_development; create database proj_test;
quit
> rake db:test:prepare
> rake db:migrate

bobby tables

http://xkcd.com/327/

Data Migrations

If you’re building data migrations, always uses .save! so that it will fail on a validation error and you may want to litter your migration with puts statements to jump to which object is failing validation. There’s probably a better way of doing this using fixtures, or using –trace to find which migration failed.

Or hell, don’t use a data migration for bulky legacy data.

There’s Something About Tests

I really like how simple it is to write fairly complicated tests. One thing I didn’t like was how many tests it is possible to write. The examples from Agile Web Development with Rails showed them creating a lot of tests for the validates_* helpers. Unfortunately, you don’t need to create tests that duplicate those helpers because they are bulletproof. You do however need tests to prove that you used them correctly.

Cut-and-paste errors do happen, and double checking my validations did reveal at least one case where I thought I was validating a field but I wasn’t. Not to mention that if you’re using a regular expression filter to validate the format of a field you might forget to put start and end delimiters on it. Even testing something simple like all values are in the list is useful because you might have another validation that invalidates one of the values from your list.

Ruby is Dynamic

One thing I can’t stress enough is how much you NEED unit tests. Ruby is a dynamic language, and as such there isn’t a great and easy way to find out if the code will blow up without running it. If you run it by hand you won’t find all the interesting scenarios for the simple reason that you won’t be rechecking features you implemented last week that exploded because of a change you made this morning. You need a regressable test suite.

And there’s nothing like writing a test to make you realize how much more complicated you’ve made things than they need to be.

How to Run Tests

Run an individual test

ruby test/unit/testname.rb

Run multiple tests:

rake test # run all test
rake test:unit # run unit tests
etc

They can all be done inside of emacs by using the Tests drop down menu in rails-mode. This is the preferred method because you can click on errors and go directly to that file.

rails-mode also lets you use C-c C-c . to run the current test file. This allows you to rapidly iterate through test development.

Running Tests – Verbose Assertions

Here’s another tip that I didn’t realize at first: you can supply a message argument to your assertions that will display when it fails. This is essential if your using loops in your tests, IE: looping over an array of invalid field values, because the line number isn’t enough information to find out why the test failed.

Debugging a Test – Breakpoint

You can use the breakpoint keyword where a test is failing. This will open up a console at the breakpoint spot. Unfortunately it doesn’t work well inside of emacs because the ROutput buffer is read only (in fact, you’ll have to kill the process). So run the test from the command line when you want to play with breakpoints. I can’t seem to find a way to access the local variables in a method… so on to ruby-debug.

Debugging a Test – rdebug (or redbug according to Microsoft Word)

sudo gem install rdebug -y

in config/environments/test.rb

require 'ruby-debug'

Then use the debugger keyword instead of the breakpoint keyword where you want to stop. Don’t use it when you’re running tests from emacs because things will look weird.

Running Tests – Fixtures – Validating Fixtures

Here’s the fun bit: sometimes you break your fixtures. Not on purpose, to test bad data, but because your erb goes a little wrong, or because they’ve gotten out of date with your schema. Here’s a rake task that will let you do rake db:fixtures:validate

If you are using erb to generate your fixtures, you can also see how your fixture will roll out using:

erb test/fixtures/fixturename.yml

And while you’re at it, you probably want to validate your existing database against your models. Here’s a rake task that will let you do rake db:validate_models

Running Tests – Advanced – autotest (part of ZenTest)

There’s a plugin called autotest that will automatically run tests on any files that have changed. This is great because you can keep the console open in the background and it will immediately catch if you’ve saved a file with a typo! No need to go to the web browser, navigate to the changed page and hit refresh. In fact, using the web browser should be an afterthought… you should be able to create tests for any features.

sudo gem install ZenTest
rehash
autotest -rails

One gotcha: disable autotest if you’re manually running tests as well! You’ll end up creating duplicate records in the test database. The solution is: don’t manually run tests with rake at the same time as autotest.

Walkthrough of autotest: http://maintainable.com/articles/dry_up_testing_with_autotest

There’s a way to integrate autotest into emacs.

Running Tests – Advanced – RedGreen (for autotest)

Not the horrible Canadian TV show, but a notifier for autotest status reports.

https://wiki.csuchico.edu/confluence/display/webd/Testing+Rails+Apps

Running Tests – Advanced – ZenTest

ZenTest is useful for parsing your rail files and creating stubs of tests.

Running Tests – Advanced – Test::Rails (part of ZenTest)

Test::Rails provides a mechanism for splitting functional tests into controller tests and view tests. This decoupled lets you check your business logic as is, and your view routing as is.

If you want to add a generator for creating view/integration/controller tests:

./script/plugin install http://topfunky.net/svn/plugins/vic_tests

Some collected thoughts about Test::Rails

Running Tests – Advanced – Rcov

Rcov is another tool that will help your testing by calculating the code coverage of your tests. This is an essential tool to find holes in your testing strategy. All the usual caveats of code coverage apply.

http://eigenclass.org/hiki.rb?rcov

sudo gem install rcov
rehash
rcov test/path_to_specific_test.rb - or - ./script/plugin install http://svn.codahale.com/rails_rcov

Now you can do

rake test:units:rcov

http://agilewebdevelopment.com/plugins/rails_rcov

Running Tests – Advanced – Heckle

Heckle mutates your code to see if the tests actually check anything. Unfortunately highly coupled code is heckle-proof because changing anything breaks everything else.

sudo gem install heckle

Running Tests – Measuring – Flog

Flog measures reports a score based on how complex it thinks your code is. The higher the score, the higher the chance that there is a bug hiding there.

sudo gem install flog

A Handful of Blogs About Rails Testing

These guys have written a lot (all?) of the plugins I’ve mentioned and are worth checking out if this stuff interests you:

Related Posts

Getting Started With Ruby on Rails – Week 1

Posted in Ruby on Rails, Technology by engtech on November 21, 2007

Learning Ruby

I’ve fallen for the hype and started using Ruby on Rails for building a database driven web application. If you’ve never heard of Rails it is a web framework using the Ruby programming language. Ruby is an object-oriented interpreted language, that’s often compared favourably with Smalltalk. [1] What’s a framework? A framework provides a structure and a set of tools usually for solving a particular type of problem. A programming language solves general problems while a framework extends a programming language to better solve a specific problem.

Rails is a framework for building web applications: stuff like blog software, instant messaging, to-do lists, web magazines, and your favorite web comic. Word on the street is that ROR is a resource hog but the resource consumption is balanced out by how much more productive it is to develop with. It’s easier to buy more computers to host a web application than it is to hire more developers. Computers get more powerful over time; developers not so much.

I’ve been developing websites as a hobby off and on since 1994, but I only learned CSS in the past six months. I’ve done some minor hacking of other people’s web apps that were written in ASP or Perl and they were always horrible messes of spaghetti code. I’m really looking forward to trying out a web app from scratch.

Choose Your Path

I run a Windows machine with a VMWare Linux box inside of it, so I can choose to do my Rails development under Windows or under Linux. If I use Windows then I can use InstantRails, which is a one-click installer that gives you everything to need to start coding ASAP. But I much prefer developing under Linux because you can’t beat the power of having a strong command line. The Windows command line console is a joke, and requires a ton of 3rd party utilities for stuff that’s already there under Linux. [2]

The downside is that there is no one-click install for Linux. Well, except for this one, which I didn’t notice until now :)

Installing ruby, gem and rails is simple and I was able to do it under my user account using the standard –prefix=/home/engtech install options.

Gotcha #1 – MySQL

I already had MySQL installed on my Linux box but it was an extremely old version that blew up the second I tried to use Rails to talk to the database. You need at least MySQL 4 to use Rails because it uses ENGINE=InnoDB for its calls. Older versions of MySQL don’t have InnoDB turned on by default, and once you do turn it on they only understand TYPE=InnoDB.

Mysql::Error: You have an error in your SQL syntax near 'ENGINE=InnoDB'

Tip: Get the latest and greatest version of MySQL instead of whatever came with your Linux install. I needed the Server, Client, and Developer RPMs. MySQL was the part of the install process that required root access.

Tip: If you use a password for your MySQL root account, make sure you change config/database.yml to use it.

Gotcha #2 – Integrated Development Environment (IDE)

Rails doesn’t come with a standard IDE, but instead gives you a wide option of choices. Aptana RadRails, based on Eclipse is a good choice. But I’ve already sold my soul to one editor for all my coding needs: emacs. Emacs is the “kitchen sink” IDE because it supports everything: you can find extensions for any programming language or task. The downside is that it has a learning curve like you wouldn’t believe.

There’s a tutorial on how to add rails support to emacs. It’s long and complicated. Using rails mode in emacs requires upgrading to emacs version 22 that broke a lot of my existing DotEmacs hacks. I eventually got it working, but in retrospect I might have been better off going with RadRails because I lost hours to this. I’m still finding emacs keystrokes that don’t do what I expected them to.

I’m unimpressed that there isn’t a quick reference print sheet for rails-mode, this is the best that I could find. So far I’ve only been using the syntax highlighting and C-c C-c g K and C-c Up / C-c Down to navigate between files.

Gotcha #3 – Development Server vs Production

When I was running into MySQL installation problems, I toyed with using SQLite3 instead for a while. Needless to say, make sure your development database is using the same versions of everything as your development and test servers. It’ll save you lots of headaches.

Initial Opinion

People weren’t lying about how productive programming with Ruby on Rails is. In the same amount of time it took me to write this blog post I was able to get a simple web application with user authentication up and running with a web interface that is probably “good enough” for final release. Which is ridiculous, compared to my previous experience hacking apps together using ASP or Perl.

  • Directory structure - Clean, clear, and everything has it’s place.
  • Naming conventions - One of the best things a framework can give is enforcing a standard way of naming things. It takes a while to learn it, but it becomes second nature that if a class is called X, the database table is called Y and the tests are called Z. If you leave it to themselves most developers create small inconsistencies in naming conventions that waste time — especially if more than one person is working on the code.
  • Don’t Repeat Yourself - I really like the way Model/View/Controller separates the code and keeps it becoming a mess. Inheritance and helpers/partials are great for keeping you from duplicating code.
  • Succinct - Wow, you really do get a lot done with very little code writing. They weren’t kidding when they said you could write blogging software in under 15 minutes.
  • HTML / CSS / XML – I really love that it doesn’t try to hide the HTML, CSS and XML under a lot of programming calls. There are helpers for doing common things, but you’re free to write your own web code.
  • Development / Test / Production - In my limited experience with web apps, I’ve never worked on anything that had more than 20 users. Testing was all done manually, and the production server was the development server. It was a mess. Clean separation makes it much easier to work on code independently and only push it out to users once it has been rigorously tested.
  • Migrations - We use to build our database tables using a PHPMyAdmin web interfaces. Needless to say, doing it through scripting where you can tear down, reassemble, and rebuild the database tables is much cleaner because everything is reproducible from scratch.
  • Rake, rdoc, and test - One of the things I like most about Ruby is that it has all the fixings I expect from modern languages: the ability to automatically generate documentation off of the code and a built-in unit testing and build framework. I’m always amazed when I see a language that doesn’t natively support these facilities.
  • Religion - The big downside to Ruby on Rails is that it feels a little bit like a religion sometimes.

Conclusion

I should have tried Ruby on Rails a long time ago. I spent entirely too much time setting up my development environment compared to when I could have been developing a web application. I could have been up and running in less than an hour if I had:

  1. Used InstantRails
  2. Used Aptana RadRails

Footnotes

1 – Did you know that Smalltalk inspired the Macintosh GUI? Smallpark was yet another example of the magic that was going on at XEROX PARC in the 70s. These are the guys who invented the mouse, colour graphic, windows/icons for a GUI, WYSIWYG text editors, Ethernet (how you talk to other computers on a network), and laser printers. Programmers at Work featured interviews with some of the people from PARC.

2 – I’m always amazed that people can program without easy access to diff, find, grep, perl, etc. All of these things are available for Windows for free, but they never work quite the way I expect them to.

Follow

Get every new post delivered to your Inbox.

Join 281 other followers