2
require 'rubygems'
require 'test/unit'

class Thing
  attr_accessor :foo

  def set_stuff
    @foo = 'bar'
  end
end

class ThingTest < Test::Unit::TestCase
  def setup
    @thing = Thing.new
  end

  def test_set_stuff
    @thing.set_stuff
    assert 'bar' == @thing.foo
  end

  def test_foo_in_other_test
    puts @thing.foo
    assert 'bar' == @thing.foo
  end
end


# Loaded suite testing
# Started
# nil
# F.
# Finished in 0.00439 seconds.
#
#   1) Failure:
# test_foo_in_other_test(ThingTest) [testing.rb:26]:
# <false> is not true.
#
# 2 tests, 2 assertions, 1 failures, 0 errors
4

2 回答 2

2

不同之处在于您没有在第二个测试中调用 @thing.set_stuff 。

于 2010-07-15T17:58:50.553 回答
1

我不像 RSpec 那样熟悉 Test::Unit,但我相信每次运行测试时都会调用 setup() 方法。所以一个@thing 将被另一个覆盖。

此外,我发现您不能假设执行测试用例的特定顺序;通常(也许一直?)测试从最后一个运行到第一个,在这种情况下似乎就是这种情况。

于 2010-07-16T03:06:41.777 回答