6

私は次のようなコードを書こうとしています:

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

ExtractionFailedの自明なサブクラスでありException、テスト/ユニットの下で、 unit.extract_from(... bad data...) を呼び出すとスローされることをアサートしようとしています

SemanticText モジュールに移動ExtractionFailedしたので、test/unit は次のようになります。

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

assert_throws(:SemanticText::ExtractionFailed) {...} を書いてみましたが、かなり紛らわしいメッセージが表示されました。TypeError: SemanticText is not a class/module

次のようにすることで機能させることができます(ハックのように見えますが):

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

では、このアサーションを Ruby で表現する正しい方法は何でしょうか?

4

1 に答える 1

7

コロンの後にシンボル名を引用符で囲みます。

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

コロンまたはその他の特殊文字を含むシンボルには、引用符が必要です。

:"SemanticText::ExtractionFailed".classirb で試してみると、それが であることがわかり、 and/orSymbolを使用する必要がなくなります。to_sto_sym

于 2009-11-24T17:01:39.287 に答える