导航

RUBY TEST UNIT问题回顾

Posted on 2011-04-26 21:29  blackwind  阅读(1017)  评论(0)    收藏  举报

最近研究RUBY TEST UNIT,心头痒,毕竟这个东西之前接触不多,于是在网上搜索了一把(哈哈,求快,在网上找现成的代码,可以少走很多冤枉路),发现了如下的代码

require  'test/unit'
require  'test/unit/ui/console/testrunner'


class MyFriend < Test::Unit::TestCase
  def setup
    @friends = ['dell', 'apple', 'sony']
  end

  def teardown
  end

  def test_add
    assert_instance_of(Array, @friends, "The @friends must be Array")

    assert_equal(3, @friends.size, "The size is not 3")

    @friends << 'acer'
    assert_equal(4, @friends.size, "The size is not 4")
  end

  def test_remove
    assert_equal(3, @friends.size, "The size is not 3")

    @friends.delete_if {|n| n == 'apple'}
    assert_equal(2, @friends.size, "The size is not 2")
  end
end

Test::Unit::UI::Console::TestRunner.run(MyFriend)

查到之后,当然是万分的高兴,毕竟连TEST UNIT是什么都还不清楚,如果到NETBEANS上运行了一把,阳光立刻的小时,荫藐随之而来;

是的,报错了,报的是“no such file to load -- test/unit/ui/console/testrunner (LoadError)”NOT  FOUND,NOT FOUND?那肯定对应的类库没安装上了,到RUBY目录的LIB下一查,确实Test::Unit下根本没有UI的目录,好了,问题找到了,不用多说立刻GOOGLE。经过2天的折磨(实在的惭愧,搜索的功力太差了),最后在rubyforge.org中找到了对应的办法,只需要使用如下安装命令“gem install test-unit-full”直接把TEST UNIT相关的类包一次打上,哈哈,类包有了一切都解决了吧,可惜,依然报错,跟之前的错一样,为什么呢?仔细一看,在NETBEANS调用类包,居然无增加上“require  'rubygems'”,接着加上;居然还是报如下的问题“`<module:Console>': uninitialized constant Test::Unit::UI::Console::Diff (NameError)”,,人生多折磨,哎,只得细细的查看rubyforge.org的使用说明,一查看,还真发现了欠缺了“gem "test-unit"”,加上重新运行之后,一切搞定

(这里需要注意,由于RUBY版本的不一致,1.8版本与1.9版本在TEST UNIT上有些不一样)

结果出来了:

Finished in 0.171875 seconds.

2 tests, 5 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

11.64 tests/s, 29.09 assertions/s