How do you test code that is not a model or controller

I have a class defined in the my Rails project libdir file that implements a bunch of utility functions as class methods. I would like to write tests against these methods using Test :: Unit and run them as part of the normal project testing procedure.

I tried to paste the file at the top level testdir ...

require 'test_helper'

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

... but that didn't work. The test was not performed.

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

+3
source share
3 answers

You can put them in your catalog 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

:

[ 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

:

[ 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

_test.rb, .

+4

/. , "" , , , ...

+7

this is an example test \ functional \ application_helper_test.rb that checks for 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
+1
source

Source: https://habr.com/ru/post/1704369/


All Articles