我有以下代码:
def game
case rand(2)
when 0
"One"
when 1
"Two"
end
end
现在我想测试它,所以我编写了以下测试:
require_relative 'random.rb'
require 'test/unit'
class StringTest < Test::Unit::TestCase
def test_respond
assert_equal("One" || "Two", game)
end
end
但是,该测试仅看到"One"
,"Two"
未见。我该怎么办呢?我应该使用||
以外的其他内容吗?还是另一个功能?
答案 0 :(得分:3)
"One" || "Two"
表达式始终评估为'One'
,因为"One"
的值为truthy
,因此它永远不会到达"Two"
部分。
你可以选择
class StringTest < Test::Unit::TestCase
def test_respond
assert %w(One Two).include?(game)
end
end