Ruby:我如何使用assert_nothing_raised?

时间:2011-01-30 16:03:39

标签: ruby unit-testing testunit

 assert_nothing_raised do
   @board.make_move(0,0,Board::HUMAN)
 end

并且文档说:

Passes if block does not throw anything.

Example:

 assert_nothing_thrown do
   [1, 2].uniq
 end

我的make_move方法:

def make_move(x, y, player)
    return false
 end

我收到错误:

test_can_make_valid_move_in_the_first_row(BoardTest):
ArgumentError: wrong number of arguments (1 for 2)

2 个答案:

答案 0 :(得分:4)

以下代码适用于我。它对你有用吗?

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

class MyTestClass < Test::Unit::TestCase
  def test_something
    assert_nothing_raised do
      p 'hi'
    end
  end
end

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

我认为你正在使用assert_nothing_raised。尝试替换

@board.make_move(0,0,Board::HUMAN)

更简单,比如

p 'hi'

看看是否有效。另外,请尝试在测试中注释掉所有其他代码。

答案 1 :(得分:3)

可以说你永远不应该使用assert_nothing_raised,因为它实际上并没有测试任何东西。参见:

http://blog.zenspider.com/blog/2012/01/assert_nothing_tested.html

我在这里发表了一些进一步的想法:

Why doesn't MiniTest::Spec have a wont_raise assertion?