{"id":7010,"date":"2014-05-17T00:20:17","date_gmt":"2014-05-17T00:20:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/struct-vs-test-double-in-ruby-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:20:17","modified_gmt":"2014-05-17T00:20:17","slug":"struct-vs-test-double-in-ruby-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/struct-vs-test-double-in-ruby-collection-of-common-programming-errors\/","title":{"rendered":"Struct vs test double in ruby-Collection of common programming errors"},"content":{"rendered":"<p><strong>Test doubles are easier to set up<\/strong><\/p>\n<pre><code>Slip = Struct.new(:id)\nslip = Slip.new(:id =&gt; 1)\n<\/code><\/pre>\n<p>vs.<\/p>\n<pre><code>slip = double('slip', :id =&gt; 1)\n<\/code><\/pre>\n<p><strong>Test doubles generate more informative error messages<\/strong><\/p>\n<pre><code>require 'spec_helper'\n\nclass TooLongError &lt; StandardError; end\n\nclass Boat\n  def moor(slip)\n    slip.moor!(self)\n  rescue TooLongError\n    false\n  end\nend\n\ndescribe Boat do\n  let(:boat) { subject }\n\n  context \"when slip won't accept boat\" do\n    it \"can't be moored\" do\n      Slip = Struct.new('Slip')\n      slip = Slip.new\n      boat.moor(slip).should be_false\n    end\n  end\nend\n<\/code><\/pre>\n<blockquote>\n<pre><code>Failure\/Error: slip.moor!(self)\n NoMethodError:\n   undefined method `moor!' for #\n<\/code><\/pre>\n<\/blockquote>\n<p>vs.<\/p>\n<pre><code>it \"can't be moored\" do\n  slip = double('slip')\n  boat.moor(slip).should be_false\nend\n<\/code><\/pre>\n<blockquote>\n<pre><code>Failure\/Error: slip.moor!(self)\n   Double \"slip\" received unexpected message :moor! with (#)\n<\/code><\/pre>\n<\/blockquote>\n<p><strong>Test doubles have better support for testing<\/strong><\/p>\n<pre><code>class Boat\n  def moor(slip)\n    slip.dont_care\n    slip.moor!(self)\n  rescue TooLongError\n    false\n  end\nend\n\nit \"can't be moored\" do\n  Slip = Struct.new('Slip')\n  slip = Slip.new\n  slip.should_receive(:moor!).and_raise(TooLongError)\n  boat.moor(slip).should be_false\nend\n<\/code><\/pre>\n<blockquote>\n<pre><code>Failure\/Error: slip.dont_care\n NoMethodError:\n   undefined method `dont_care' for #\n<\/code><\/pre>\n<\/blockquote>\n<p>vs.<\/p>\n<pre><code>it \"can't be moored\" do\n  slip = double('slip').as_null_object\n  slip.should_receive(:moor!).and_raise(TooLongError)\n  boat.moor(slip).should be_false\nend\n<\/code><\/pre>\n<blockquote>\n<p><code>0 failures # passed!<\/code><\/p>\n<\/blockquote>\n<p>That&#8217;s a few examples &#8212; I&#8217;m sure there are more reasons to prefer test doubles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Test doubles are easier to set up Slip = Struct.new(:id) slip = Slip.new(:id =&gt; 1) vs. slip = double(&#8216;slip&#8217;, :id =&gt; 1) Test doubles generate more informative error messages require &#8216;spec_helper&#8217; class TooLongError &lt; StandardError; end class Boat def moor(slip) slip.moor!(self) rescue TooLongError false end end describe Boat do let(:boat) { subject } context &#8220;when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7010","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7010"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7010\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}