<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[Posts tagged 'Ruby' — Typing with mittens on]]></title><description><![CDATA[Rachel Evans writes about tech, Denmark, and probably other stuff]]></description><link>https://rachelevans.org/blog/tag/ruby/</link><image><url>https://rachelevans.org/blog/assets/favicon.png</url><title>Posts tagged &apos;Ruby&apos; — Typing with mittens on</title><link>https://rachelevans.org/blog/tag/ruby/</link></image><generator>RSS for Node</generator><lastBuildDate>Wed, 18 Feb 2026 09:07:06 GMT</lastBuildDate><atom:link href="https://rachelevans.org/blog/tag/ruby/rss/" rel="self" type="application/rss+xml"/><pubDate>Wed, 18 Feb 2026 09:07:06 GMT</pubDate><copyright><![CDATA[Copyright 2026 Rachel Evans]]></copyright><language><![CDATA[en-gb]]></language><managingEditor><![CDATA[Rachel Evans]]></managingEditor><webMaster><![CDATA[Rachel Evans]]></webMaster><ttl>180</ttl><item><title><![CDATA[rspec and exceptions]]></title><description><![CDATA[A surprising way in which an exception doesn't always cause a test to fail. Or indeed have run at all...]]></description><link>https://rachelevans.org/blog/rspec-and-exceptions/</link><guid isPermaLink="false">5c998fbe11b34b000133e705</guid><category><![CDATA[technology]]></category><category><![CDATA[Ruby]]></category><dc:creator><![CDATA[Rachel Evans]]></dc:creator><pubDate>Tue, 12 Sep 2017 08:00:00 GMT</pubDate><content:encoded><![CDATA[<p>Today I learnt that these two tests are, not only non-functionally different, but functionally different too:</p><pre><code>Test A:

    it &quot;should run without raising an exception&quot; do
      some_code_to_test
    end

    Test B:

    it &quot;should run without raising an exception&quot; do
      expect {
        some_code_to_test
      }.not_to raise_error
    end</code></pre><p>Like JUnit, rspec reacts to tests which raise exceptions by failing the test,  right? So these two tests should (functionally) behave identically?</p><p>Wrong!</p><p>Well, <em>sometimes</em> wrong.</p><p>The difference (or rather, at least one of the differences — perhaps there are more) lies in <code>SystemExit</code>. If <code>some_code_to_test</code> raises this, typically by calling <code>Kernel#exit</code>,  then the test runner stops, treats this test as successful, doesn’t  show the output of this test, and silently skips all subsequent tests:</p><pre><code>it &quot;runs test 1&quot; do
    end

    it &quot;should run without raising an exception (2)&quot; do
      some_code_to_test
    end

    it &quot;runs test 3&quot; do
    end

    // and then run it:

    rachel@shinypig test$ bundle exec rspec --format doc spec/my_spec.rb

    Kernel
      runs test 1

    Finished in 0.00089 seconds (files took 0.08703 seconds to load)
    2 examples, 0 failures</code></pre><p>which reports that it ran 2 examples, but only shows one of them, and doesn’t even mention the one that it skipped completely.</p><p>On the other hand, if we wrap the code being tested using “expect … not_to raise_error”:</p><pre><code>rachel@shinypig test$ bundle exec rspec --format doc spec/my_spec.rb

    Kernel
      runs test 1
      should run without raising an exception (2) (FAILED - 1)
      runs test 3

    Failures:

    1) Kernel should run without raising an exception (2)
         Failure/Error: expect { some_code_to_test }.not_to raise_error

    expected no Exception, got #&amp;lt;SystemExit: exit&amp;gt; with backtrace:
             # ./spec/my_spec.rb:4:in `exit&#x27;
             # ./spec/my_spec.rb:4:in `some_code_to_test&#x27;
             # ./spec/my_spec.rb:11:in `block (3 levels) in &amp;lt;top (required)&amp;gt;&#x27;
             # ./spec/my_spec.rb:11:in `block (2 levels) in &amp;lt;top (required)&amp;gt;&#x27;
         # ./spec/my_spec.rb:11:in `block (2 levels) in &amp;lt;top (required)&amp;gt;&#x27;

    Finished in 0.01099 seconds (files took 0.07536 seconds to load)
    3 examples, 1 failure

    Failed examples:

    rspec ./spec/my_spec.rb:10 # Kernel should run without raising an exception (2)</code></pre><p>So now it runs all three tests, showing all three results, including one failure.</p><p>To  be honest I’m surprised that the rspec test runner doesn’t deal with this natively: if a test raises an error, it should always be a failure (to my mind anyway), including <code>SystemExit</code>. Maybe there’s a bug / pull request for rspec somewhere discussing this, where the idea was rejected. I might go digging…</p>]]></content:encoded></item></channel></rss>