I'm trying to write code like this:
我正在尝试编写这样的代码:
assert_throws(:ExtractionFailed) { unit.extract_from('5 x 2005')}
ExtractionFailed
is a trivial subclass of Exception
, and under test/unit, I'm trying to assert that it is thrown when I call unit.extract_from(... bad data...)
ExtractionFailed是Exception的一个简单子类,在test / unit下,我试图断言当我调用unit.extract_from时抛出它(...坏数据......)
I've moved ExtractionFailed
into the SemanticText module, so now test/unit says:
我已将ExtractionFailed移动到SemanticText模块中,所以现在test / unit说:
<:ExtractionFailed> expected to be thrown but
<:"SemanticText::ExtractionFailed"> was thrown.
I tried writing assert_throws(:SemanticText::ExtractionFailed) {...} but I got the rather confusing message: TypeError: SemanticText is not a class/module
我尝试编写assert_throws(:SemanticText :: ExtractionFailed){...}但是我收到了相当混乱的消息:TypeError:SemanticText不是类/模块
I can make it work by doing the following (although it seems like a hack):
我可以通过执行以下操作使其工作(虽然它看起来像一个黑客):
assert_throws(SemanticText::ExtractionFailed.to_s.to_sym) { unit.extract_from('5 x 2005')}
So what's the right way to say this assertion in ruby?
那么在红宝石中说这个断言的正确方法是什么?
1 个解决方案
#1
7
Put quotes around the symbol name after the colon e.g.
在冒号后面的符号名称周围加上引号,例如
assert_throws(:"SemanticText::ExtractionFailed") { unit.extract_from('5 x 2005')}
The quotes are necessary for a symbol that contains colons or other special characters.
对于包含冒号或其他特殊字符的符号,引号是必需的。
If you try :"SemanticText::ExtractionFailed".class
in irb you will see that it is a Symbol
, removing the need to use to_s
and/or to_sym
.
如果您尝试:irb中的“SemanticText :: ExtractionFailed”.class,您将看到它是一个符号,无需使用to_s和/或to_sym。
#1
7
Put quotes around the symbol name after the colon e.g.
在冒号后面的符号名称周围加上引号,例如
assert_throws(:"SemanticText::ExtractionFailed") { unit.extract_from('5 x 2005')}
The quotes are necessary for a symbol that contains colons or other special characters.
对于包含冒号或其他特殊字符的符号,引号是必需的。
If you try :"SemanticText::ExtractionFailed".class
in irb you will see that it is a Symbol
, removing the need to use to_s
and/or to_sym
.
如果您尝试:irb中的“SemanticText :: ExtractionFailed”.class,您将看到它是一个符号,无需使用to_s和/或to_sym。