<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>PaulBarry.com - Displaying error messages in RSpec failures</title>
  <subtitle type="html">My thoughts, ideas, questions and concerns on technology, sports, music and life</subtitle>
  <id>tag:paulbarry.com,2007:Paulbarry.com</id>
  <generator uri="http://www.paulbarry.com" version="3.0">PaulBarry.com</generator>
  <link href="http://paulbarry.com/xml/atom/article/4894/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://paulbarry.com/articles/2008/04/23/displaying-error-messages-in-rspec-failures" rel="alternate" type="text/html"/>

  <updated>2008-11-06T06:38:16-05:00</updated>
  <entry>
    <author>
      <name>Paul Barry</name>
      <email>mail@paulbarry.com</email>
    </author>
    <id>urn:uuid:ba6d0a95-0525-4bd6-9f44-d5663e6fde88</id>

    <published>2008-04-23T18:30:13-04:00</published>
    <updated>2008-04-23T18:30:13-04:00</updated>
    <title type="html">Displaying error messages in RSpec failures</title>
    <link href="http://paulbarry.com/articles/2008/04/23/displaying-error-messages-in-rspec-failures" rel="alternate" type="text/html"/>

    <category term="technology" scheme="http://paulbarry.com/articles/category/technology" label="Technology"/>
        <category term="RSpec" scheme="http://paulbarry.com/articles/tag/rspec"/>
    <category term="Rails" scheme="http://paulbarry.com/articles/tag/rails"/>
    <category term="Ruby" scheme="http://paulbarry.com/articles/tag/ruby"/>
        <summary type="html">&lt;p&gt;The &lt;a href=&quot;http://rspec.info/documentation/rails/writing/models.html&quot;&gt;suggested way of checking if an ActiveRecord model has errors with RSpec&lt;/a&gt; is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@something.errors.should be_empty
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When that fails, you get a report that says:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;expected empty? to return true, got false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&apos;s not very helpful because it doesn&apos;t tell you what the errors are.  If you do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@something.errors.full_messages.should == []
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You get a more informative failure message:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;expected: [],
got: [&quot;Whatever can&apos;t be blank&quot;] (using ==)
&lt;/code&gt;&lt;/pre&gt;
</summary>
    <content type="html">&lt;p&gt;The &lt;a href=&quot;http://rspec.info/documentation/rails/writing/models.html&quot;&gt;suggested way of checking if an ActiveRecord model has errors with RSpec&lt;/a&gt; is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@something.errors.should be_empty
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When that fails, you get a report that says:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;expected empty? to return true, got false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&apos;s not very helpful because it doesn&apos;t tell you what the errors are.  If you do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@something.errors.full_messages.should == []
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You get a more informative failure message:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;expected: [],
got: [&quot;Whatever can&apos;t be blank&quot;] (using ==)
&lt;/code&gt;&lt;/pre&gt;
</content>
  </entry>
  <entry>
    <author>
      <name>nicholas a. evans</name>
    </author>
    <id>urn:uuid:f7ea3d87-c8c2-4012-8423-9f188c2d99f3</id>
    <published>2008-04-24T13:39:33-04:00</published>
    <updated>2008-04-24T13:39:33-04:00</updated>
    <title type="html">Comment on "Displaying error messages in RSpec failures" by nicholas a. evans</title>
    <link href="http://paulbarry.com/articles/2008/04/23/displaying-error-messages-in-rspec-failures#comment-5111" rel="alternate" type="text/html"/>
    <content type="html">Or you could use my ValidationSpecHelper module, which could be called like so:&lt;br/&gt;&lt;br/&gt;&amp;lt;pre&amp;gt;&lt;br/&gt;@it.should accept(&amp;quot;blah&amp;quot;).for(:firstname)&lt;br/&gt;@it.should accept(:firstname).of(&amp;quot;foobar&amp;quot;)&lt;br/&gt;@it.should reject(:firstname).of(&amp;quot;&amp;quot;)&lt;br/&gt;@it.should reject(:firstname).of(&amp;quot;chunky&amp;quot;).with(&amp;quot;reason&amp;quot;)&lt;br/&gt;&amp;lt;/pre&amp;gt;&lt;br/&gt;&lt;br/&gt;and return failure messages like so:&lt;br/&gt;&lt;br/&gt;&amp;lt;pre&amp;gt;&lt;br/&gt;expected validation to accept &amp;quot;&amp;quot; for firstname&lt;br/&gt;but firstname had the following errors:&lt;br/&gt;- firstname: can&apos;t be blank&lt;br/&gt;&lt;br/&gt;expected validation to reject &amp;quot;a name&amp;quot; for firstname&lt;br/&gt;but firstname was valid&lt;br/&gt;&amp;lt;/pre&amp;gt;&lt;br/&gt;&lt;br/&gt;and so on...&lt;br/&gt;&lt;br/&gt;http://svn.ekenosen.net/public/rspec_validation_matcher/&lt;br/&gt;&lt;br/&gt;There are other validation spec matchers out there, but I like mine the best.  ;-)</content>
  </entry>
  <entry>
    <author>
      <name>nicholas a. evans</name>
    </author>
    <id>urn:uuid:ee85154f-879f-4246-a581-0c66ffb69f70</id>
    <published>2008-04-24T13:40:15-04:00</published>
    <updated>2008-04-24T13:40:15-04:00</updated>
    <title type="html">Comment on "Displaying error messages in RSpec failures" by nicholas a. evans</title>
    <link href="http://paulbarry.com/articles/2008/04/23/displaying-error-messages-in-rspec-failures#comment-5112" rel="alternate" type="text/html"/>
    <content type="html">Oh well, I guess the &amp;lt;pre&amp;gt; tags didn&apos;t work.  Preview buttons are nice things for blog comments.  ;-)</content>
  </entry>
  <entry>
    <author>
      <name>bryanl</name>
    </author>
    <id>urn:uuid:589247b1-9f61-442c-a984-889fcc2f289f</id>
    <published>2008-04-25T06:58:31-04:00</published>
    <updated>2008-04-25T06:58:31-04:00</updated>
    <title type="html">Comment on "Displaying error messages in RSpec failures" by bryanl</title>
    <link href="http://paulbarry.com/articles/2008/04/23/displaying-error-messages-in-rspec-failures#comment-5113" rel="alternate" type="text/html"/>
    <content type="html">Or you just give up on rspec and its spaghetti mess of code and start using shoulda.  &lt;br/&gt;&lt;br/&gt;should_not_allow_values_for :email, &amp;quot;blah&amp;quot;, &amp;quot;b lah&amp;quot; &lt;br/&gt;should_allow_values_for :email, &amp;quot;a@b.com&amp;quot;, &amp;quot;asdf@asdf.com&amp;quot; &lt;br/&gt;</content>
  </entry>
  </feed>