我在一个文件中定义了一个类,我的Rails项目的lib目录将实现一堆实用函数作为类方法.我想使用Test :: Unit编写针对这些方法的测试,并将它们作为项目常规测试例程的一部分运行. 我试着在
我试着在测试目录的顶层粘贴一个文件…
require 'test_helper' class UtilsTest < ActiveSupport::TestCase should 'be true' do assert false end end
……但那没用.测试没有运行.
测试代码的正常方法是什么,而不是Rails的常规部分之一?
您可以将它们放在您的单位目录中,而不会出现任我的图书馆:
[ 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中结束,就应该把它拿起来.