0

情况很简单。我有一个这样的模块(first.rb):

def ret_s(l)
  return "one" if l == 1
  return "twoe" if l == 2
  return "threee" if l == 3
end


def ret_l(str)
  return 1 if str == "one" 
  return 2 if str == "two"  
  return 4 if str == "three" 
end

和一个测试类(TestFist.rb):

require 'test/unit'
require './first'

class TestFirst < Test::Unit::TestCase

  def test_fist

    assert_equal("one",ret_s(1))
    assert_equal("two",ret_s(2))
    assert_equal("three",ret_s(3))
  end

  def test_second

    assert_equal(1,ret_l("one"))
    assert_equal(2,ret_l("two"))
    assert_equal(3,ret_l("three"))
  end

end

运行此测试时,为什么我会得到这样的答案:

$ ruby TestFirst.rb
Run options:

# Running tests:

FF

Finished tests in 0.001000s, 2000.0000 tests/s, 5000.0000 assertions/s.

  1) Failure:
test_fist(TestFirst) [TestFirst.rb:9]:
<"two"> expected but was
<"twoe">.

  2) Failure:
test_second(TestFirst) [TestFirst.rb:17]:
<3> expected but was
<4>.

2 tests, 5 assertions, 2 failures, 0 errors, 0 skips

最后一行让我觉得我做错了什么。我会期待这样的事情:

2 次测试,6 次断言,3 次失败,0 次错误,0 次跳过

不是

2 次测试,5 次断言,2 次失败,0 次错误,0 次跳过

4

1 回答 1

2

第一个测试在第一个失败的断言之后中止,因此它永远不会执行该行assert_equal("three",ret_s(3))

于 2013-07-24T16:34:31.410 回答