Ruby:如何使用assert_nothing_raised?

发布于 2024-10-15 13:17:12 字数 477 浏览 5 评论 0原文

 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)
 assert_nothing_raised do
   @board.make_move(0,0,Board::HUMAN)
 end

and the docs say:

Passes if block does not throw anything.

Example:

 assert_nothing_thrown do
   [1, 2].uniq
 end

my make_move method:

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

I get the error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹泡泡o 2024-10-22 13:17:12

下面的代码对我有用。它对你有用吗?

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'

是否有效。还可以尝试注释掉测试中的所有其他代码。

This following code works for me. Does it work for you?

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)

I think you are using assert_nothing_raised correctly. Try replacing

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

with something more simple, like

p 'hi'

and see if that works. Also try commenting out all the other code in your test.

于我来说 2024-10-22 13:17:12

可以说,您永远不应该使用 assert_nothing_raised,因为它实际上不会测试任何内容。请参阅:

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

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

为什么 MiniTest::Spec 没有 wont_raise 断言?

It can be argued that you should never use assert_nothing_raised, as it does not actually test anything. See:

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

I've posted some further thoughts here:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文