Unit Testing the file_column ruby on rails plugin
Saturday, July 22nd, 2006Fun times with unit testing - had to make two small changes to be able to unit test the file_column plugin. The first was easy, just changing the database connection settings in the connection.rb file in the test directory. The next change took a little longer (not being familiar with ruby syntax). After the database connection was fine, the dependencies for the actual file_column couldn’t be resolved.
In abstract_unit.rb the line
$: << "../lib"
is at the top of the file. From the Ruby predefined variables link on the delicious post for today the $: << dir adds directories to be searched when looking for required classes. It's like adding directories to the classpath for a Java program. I was running the tests with rake test_plugins and the dependency was failing - I'm assuming running with rake doesn't change to the actual directory the file is in. To fix the problem I added:
$: << File.expand_path(File.dirname(__FILE__)+"/../lib")
which gives the absolute path of the required files. File.dirname(__FILE__) is the path of the current file we’re in and the expand_path cleans up the directory name to go to the parent directory and append the lib directory name.
Simple problem solved now moving on to reach 100% coverage of tests/code…