Ruby 如何测试具有多个可选输出的方法?
我有以下代码:Ruby 如何测试具有多个可选输出的方法?,ruby,Ruby,我有以下代码: 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", gam
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
require_relative'random.rb'
需要“测试/单元”
类StringTest
但是,测试只看到“一”
,“二”
。我怎么能修好它?我应该使用除| |
以外的东西吗?还是其他功能
"One" || "Two"
表达式总是计算为'One'
,因为“One”
是truthy
值,所以它永远不会到达“Two”
部分
你可以和我一起去
class StringTest < Test::Unit::TestCase
def test_respond
assert %w(One Two).include?(game)
end
end
classstringtest
表达式总是计算为'One'
,因为“One”
是truthy
值,所以它永远不会到达“Two”
部分
你可以和我一起去
class StringTest < Test::Unit::TestCase
def test_respond
assert %w(One Two).include?(game)
end
end
classstringtest