我在codeschool上关注此代码
string_extension_test.rb
gem "minitest", "~> 4.0"
require "test/unit"
require "string_extension"
assert "3".is_number?
assert !"Blash".is_number?
string_extension.rb
class String
def is_number?
if self =~/^\d+$/
true
else
false
end
end
end
我输入ruby -I. string_extension_test.rb
但是,它显示了
string_extension_test.rb:6:in `<main>': undefined method `assert' for main:Object (NoMethodError)
我尝试在 string_extension_test.rb 中添加include Test::Unit::Assertions
,它可以通过,但没有测试用例。
输出将是
Finished tests in 0.013038s, 0.0000 tests/s, 0.0000 assertions/s.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
答案 0 :(得分:6)
您可能需要将测试放在继承自Test :: Unit :: TestCase的类中。类似的东西:
class StringExtensionTest < Test::Unit::TestCase
def test_is_number
assert '3'.is_number?
end
def test_is_not_number
assert !'B'.is_number?
end
end
答案 1 :(得分:4)
将测试放在一个类中......确保方法以字符串“test”...
开头gem "minitest", "~> 4.0"
require 'test/unit'
require 'calc'
class TestStringExtension < Test::Unit::TestCase
def test_is_number?
assert "3".is_number?
assert !"Blash".is_number?
end
end