{"data":{"wordpressPost":{"id":"2278cb8d-bc57-57b9-bceb-c2dd540551c5","title":"RSpec 3 Cheatsheet","slug":"rspec-3-cheatsheet","content":"<p>This is a quick and dirty reference guide to the absolute basics of RSpec 3.</p>\n<p>My experience has been that beginner programmers don&#8217;t get enough practice with testing, and that a lot of RSpec references don&#8217;t specify which version they&#8217;re using and aren&#8217;t necessarily updated to reflect the evolution of best practices (like the switch from <code>should</code> to <code>expect</code>).</p>\n<h2>Describe It</h2>\n<p>The general pattern to follow is to have a <code>describe</code> statement that wraps around <code>it</code> statements which do the actual heavy lifting:</p>\n<blockquote>\n<pre>describe Order do\r\n  it \"human-readable does-something description\" do\r\n    # expect statement - your test! - goes here\r\n  end\r\nend</pre>\n</blockquote>\n<p>Only one expectation per <code>it</code> statement!</p>\n<h2>Contextualize</h2>\n<p>You can also create a <code>context</code> to organize your tests according to the various situations you&#8217;re testing:</p>\n<blockquote>\n<pre>describe Order do\r\n  context \"invalid user\" do\r\n    it \"does not add item to cart\" do\r\n      # expect statement\r\n    end\r\n\r\n    it \"redirects to user login\" do\r\n      # expect statement\r\n    end\r\n  end\r\nend</pre>\n</blockquote>\n<h2>Matchy-Matchy</h2>\n<p>There are a variety of kinds of things you can test for, and some of them are easy to mix up with each other. Some <a title=\"Built in matchers\" href=\"http://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers\" target=\"_blank\">built-in matchers</a> for testing objects:</p>\n<blockquote>\n<pre>expect(something).to eq(something)    # equivalence\r\nexpect(something).to be(something)    # identity\r\n\r\nexpect(something).to be false         # value is false\r\nexpect(something).to be_falsey        # value is \"falsey\"</pre>\n</blockquote>\n<p>When you want to test something other than an object, you can use a block:</p>\n<blockquote>\n<pre>expect{\r\n  # do something\r\n  }.to change {something}.by(1)\r\n\r\nexpect{\r\n  # do something\r\n  }.to raise_exception(ExceptionName)</pre>\n</blockquote>\n<h3>Resources</h3>\n<p>There are quite a few resources out there that do an excellent job of <em>contradicting each other</em>, so try to get at least a couple different opinions on something (like <code>before</code> vs <code>let</code>) before you use it!</p>\n<ul>\n<li>Abi Noda&#8217;s <a href=\"https://github.com/abinoda/rspec-best-practices\" target=\"_blank\">Rspec Best Practices</a> is a good place to start</li>\n<li>Relish&#8217;s <a href=\"http://www.relishapp.com/rspec/rspec-expectations/v/3-2/docs\" target=\"_blank\">RSpec Expectations 3.2</a> &#8211; the actual docs are less painful than some</li>\n<li><a href=\"http://betterspecs.org/\" target=\"_blank\">BetterSpecs.org</a> has examples of good vs. bad implementation</li>\n<li>Practice using RSpec with Rails via the <a href=\"http://everydayrails.com/rspecbook/\" target=\"_blank\">Everyday Rails</a> book (there&#8217;s a link to the free PDF at the bottom &#8211; I&#8217;ve only read the PDF and would have preferred it took a TDD-from-the-very-beginning approach, but it&#8217;s still good for RSpec practice)</li>\n</ul>\n","date":"June 30, 2014","categories":[{"name":"ones and zeros","slug":"ones-and-zeros"}],"tags":null,"author":{"name":"jessica","slug":"jessica"}}},"pageContext":{"id":"2278cb8d-bc57-57b9-bceb-c2dd540551c5"}}