How do you claim that an exception from another ruby ​​module is thrown? (using assert_throws)

I am trying to write code like this:

assert_throws(:ExtractionFailed) { unit.extract_from('5 x 2005')} 

ExtractionFailed is a trivial subclass of Exception , and when testing / module I try to claim that it throws when I call unit.extract_from (... bad data ...)

I moved ExtractionFailed to the SemanticText module, so now test / unit says:

 <:ExtractionFailed> expected to be thrown but <:"SemanticText::ExtractionFailed"> was thrown. 

I tried to write assert_throws (: SemanticText :: ExtractionFailed) {...}, but I got a rather confusing message: TypeError: SemanticText is not a class/module

I can get it working by doing the following (although it seems to be a hack):

  assert_throws(SemanticText::ExtractionFailed.to_s.to_sym) { unit.extract_from('5 x 2005')} 

So what is the right way to say this statement in ruby?

+6
ruby module symbol testunit
source share
1 answer

Put quotation marks around the character name after the colon, for example.

 assert_throws(:"SemanticText::ExtractionFailed") { unit.extract_from('5 x 2005')} 

Quotation marks are required for characters containing colons or other special characters.

If you try :"SemanticText::ExtractionFailed".class in irb, you will see that it is Symbol , eliminating the need to use to_s and / or to_sym .

+7
source share

All Articles