views:

191

answers:

3

I have a class defined in a file my Rails project's lib dir that will implement a bunch of utility functions as class methods. I'd like to write tests against those methods using Test::Unit and have them run as part of the project's regular testing routine.

I tried sticking a file at the top level of the test dir...

require 'test_helper'

class UtilsTest < ActiveSupport::TestCase
  should 'be true' do
    assert false
  end
end

...but that didn't work. The test didn't run.

What is the normal way to test code that is not one of the regular parts of Rails?

+7  A: 

I just stick them in test/unit. I'm not sure if that's the "correct" approach, but it seems to work for me...

Scotty Allen
+4  A: 

You can put them in your units directory without any complications.

My Library:

[ cpm juno ~/apps/feedback ] cat lib/my_library.rb
class MyLib
  def look_at_me
    puts "I DO WEIRD STUFF"
  end
end

My Test:

[ cpm juno ~/apps/feedback ] cat test/unit/fake_test.rb
require 'test_helper'

class FakeTest < ActiveSupport::TestCase
  # Replace this with your real tests.
  def test_truth
    require 'my_library'

    puts "RUNNING THIS NOW"
    MyLib.new.look_at_me
    assert true
  end
end

And running it:

[ cpm juno ~/apps/feedback ] rake test:units
(in /home/cpm/apps/feedback)
/usr/bin/ruby1.8 -Ilib:test "/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/unit/fake_test.rb"
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
RUNNING THIS NOW
I DO WEIRD STUFF
.
Finished in 0.254404 seconds.

1 tests, 1 assertions, 0 failures, 0 errors
Loaded suite /var/lib/gems/1.8/bin/rake
Started

Finished in 0.00094 seconds.

0 tests, 0 assertions, 0 failures, 0 errors

As long as it ends in _test.rb it should pick it up.

cpm
+1  A: 

this is an example of test\functional\application_helper_test.rb that test the helper functionality

require File.dirname(__FILE__) + '/../test_helper'

require 'application_helper'

class HelperTest < Test::Unit::TestCase
    include ApplicationHelper

    def test_clear_search_string
        assert_equal 'Park Ave', clear_search_string('street Park Ave')
    end
end
se_pavel