Unit Testing the file_column ruby on rails plugin

Fun 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…

One Response to “Unit Testing the file_column ruby on rails plugin”

  1. JC Says:

    Yeah so another note - I still had 4 unit tests failing for file column. In the abstract_unit.rb class the RAILS_ENV was set to:
    RAILS_ENV=”"
    and I changed it to:
    RAILS_ENV=”test”
    thinking, hey that’d be a great idea. Not so good. In test_case.rb the default path for images is changed if in the test environment:
    # If we are running in the “test” environment, we overwrite the default
    # settings for FileColumn so that files are not uploaded into “/public/”
    # in tests but rather into the directory “/test/tmp/file_column”.
    if RAILS_ENV == “test”
    FileColumn::ClassMethods::DEFAULT_OPTIONS[:root_path] =
    File.join(RAILS_ROOT, “test”, “tmp”, “file_column”)
    end
    which will cause 4 tests to fail/error out. The RAILS_ROOT is set in abstract_unit.rb to have the images saved in the same directory as abstract_unit, so the line in test_case.rb is redundant and unnecessary.

Leave a Reply