我正试着通过Koans学习Ruby,但是我被困在了第六步。
代码如下:
def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
# What happens when you call a method that doesn't exist.
# The following begin/rescue/end code block captures the exception and
# make some assertions about it.
begin
nil.some_method_nil_doesnt_know_about
rescue Exception => ex
# What exception has been caught?
assert_equal __, ex.class
# What message was attached to the exception?
# (HINT: replace __ with part of the error message.)
assert_match(/__/, ex.message)
end
end我知道我应该用与错误消息"NoMethodError“有关的东西来替换__,但我似乎搞不清楚。
这是我在运行“path_to_enlightenment.rb”时收到的错误消息:
The answers you seek...
<"FILL ME IN"> expected but was <NoMethodError>.我真的很感激这方面的一些指导-它快把我逼疯了!我很想知道答案和可能的解释。谢谢!
发布于 2010-09-29 23:06:33
这里的答案是"NoMethodError“
你需要两边的项是相等的,因此将它们都设为ex.class可以做到这一点。
然后,您需要转到下面的/__/。
发布于 2011-05-27 03:40:32
我必须将assert_equal语句放在括号中才能使这条语句通过。一定是个窃听器。
def test_you_dont_get_null_pointer_errors_when_calling_methods_on_nil
# What happens when you call a method that doesn't exist. The
# following begin/rescue/end code block captures the exception and
# make some assertions about it.
begin
nil.some_method_nil_doesnt_know_about
rescue Exception => ex
# What exception has been caught?
assert_equal(NoMethodError, ex.class)
# What message was attached to the exception?
# (HINT: replace __ with part of the error message.)
assert_match("undefined method", ex.message)
end
end发布于 2012-02-20 15:27:30
需要将__替换为实际的
assert_equal NoMethodError, ex.class https://stackoverflow.com/questions/3818561
复制相似问题