I'm working on this Exercise 49 in Learn Ruby the Hard Way
The exercise asks to write a unit test for each function provided. One of the unit tests I am writing is giving me an error.
Here is the code I am testing (word_list is an array of Pair structs)
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
Here is the test:
def test_skip
small_list = [Pair.new(:verb, 'go'), Pair.new(:verb, 'do')]
skip(small_list, :verb)
assert_equal([],small_list)
end
Here is the error I get when I run the unit test:
1) Error:
test_skip(SentenceTests):
TypeError: backtrace must be Array of String
test_sentence.rb:23:in `test_skip'
In this case, line 23 refers to "skip(small_list, :verb)". I am not sure why this error is happening, the above two functions were unit tested as well and those tests came out fine.
@Zabba, I did put them exactly as specified in the exercise above:
class ParserError < Exception
end
If anyone needs to see the exact files I am using here is the link to a gist: https://gist.github.com/1190148
The line
skip(small_list, :verb)
makes your test skip through the whole list (2 verbs). Finally, it is empty.
So small_list.first
is nil
which has no method/field word
.
I get the error
undefined method `word' for nil:NilClass (NoMethodError)
though.
Probably in your code this exception is caught by some other code which itself raises the exception you are seeing.
UPDATE
It turns out in Ruby 1.9's test/unit (more specifically in the minitest/unit it is based on), there is a method skip
which collides with your skip
. See minitest/unit.rb, line 610. That method raised the exception because it expected its 2nd parameter to be an array.
Just rename your skip
.