Learn Ruby the Hard Way の演習 49 に取り組んでいます。
この演習では、提供される各機能の単体テストを作成するよう求められます。私が書いている単体テストの 1 つでエラーが発生しています。
これが私がテストしているコードです(word_listはペア構造体の配列です)
Pair = Struct.new(:token, :word)
def peek(word_list)
begin
word_list.first.token
rescue
nil
end
end
def match(word_list, expecting)
begin
word = word_list.shift
if word.token == expecting
word
else
nil
end
rescue
nil
end
end
def skip(word_list, token)
while peek(word_list) == token
match(word_list, token)
end
end
テストは次のとおりです。
def test_skip
small_list = [Pair.new(:verb, 'go'), Pair.new(:verb, 'do')]
skip(small_list, :verb)
assert_equal([],small_list)
end
単体テストを実行すると、次のエラーが表示されます。
1) Error:
test_skip(SentenceTests):
TypeError: backtrace must be Array of String
test_sentence.rb:23:in `test_skip'
この場合、23行目は「skip(small_list, :verb)」を参照しています。このエラーが発生する理由はわかりません。上記の 2 つの関数も単体テストされており、それらのテストは問題なく行われました。
@Zabba、上記の演習で指定したとおりにそれらを配置しました。
class ParserError < Exception
end
私がここで使用している正確なファイルを誰かが見る必要がある場合は、要点へのリンクです: https://gist.github.com/1190148