title: Proper Assert_Raise Unit Testing and Use of Exception Class
body: <p>I am working on <a href="http://ruby.learncodethehardway.org/book/ex49.html" rel="noreferrer">Exercise 49 of Learn Ruby the Hard Way</a></p>
  
  <p>The exercise asks to write a unit test for each function provided. One of the items I am testing is if a proper exception is raised. It is suggested that we use <code>assert_raise</code> for this purpose.</p>
  
  <p>Here is the code I am testing:</p>
  
  <pre><code>class ParserError &lt; Exception
  
  end
  
  Pair = Struct.new(:token, :word)
  
  def peek(word_list)
      begin
          word_list.first.token
      rescue
          nil
      end 
  end
  
  def match(word_list, expecting)
      word = word_list.shift
      if word.token == expecting
          word
      else
          nil
      end
  end
  
  def skip_word(word_list, token)
      while peek(word_list) == token
          match(word_list, token)
      end
  end
  
  def parse_verb(word_list)
      skip_word(word_list, :stop)
  
      if peek(word_list) == :verb
          return match(word_list, :verb)
      else
          raise ParserError.new("Expected a verb next.")
      end
  end
  </code></pre>
  
  <p>And here is the test, for the function parse_verb:</p>
  
  <pre><code>def test_parse_verb
      list_one = [Pair.new(:verb, 'go'), Pair.new(:noun, 'king')]
      assert_equal(parse_verb(list_one), Pair.new(:verb, 'go'))
  
      list_two = [Pair.new(:noun, 'player') ,Pair.new(:verb, 'go'), Pair.new(:noun, 'king')]
      assert_raise(ParserError.new("Expected a verb next.")) {parse_verb(list_two)}
  end
  </code></pre>
  
  <p>When I run the test, it fails and here is the message I get:</p>
  
  <pre><code>Larson-2:test larson$ ruby test_sentence.rb
  Loaded suite test_sentence
  Started
  .F..
  Finished in 0.001204 seconds.
  
    1) Failure:
  test_parse_verb(SentenceTests) [test_sentence.rb:36]:
  [#&lt;ParserError: Expected a noun or direction next.&gt;] exception expected, not
  Class: &lt;ParserError&gt;
  Message: &lt;"Expected a verb next."&gt;
  ---Backtrace---
  /Users/larson/Ruby/projects/ex48/lib/sentence.rb:45:in `parse_verb'
  test_sentence.rb:36:in `block in test_parse_verb'
  ---------------
  
  4 tests, 7 assertions, 1 failures, 0 errors, 0 skips
  
  Test run options: --seed 40627
  </code></pre>
  
  <p>Based on my understanding of the <code>assert_raise</code> function, this test should pass, is there something wrong with the way I am using it?</p>
  
  <p>If anybody would like a full source code of all the files I am working with I it is available <a href="https://gist.github.com/1190148/79f80493064453d94cffc75f7374fbf65e83485b" rel="noreferrer">here</a></p>