<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>An Experiment in Bloggery &#187; RSpec</title>
	<atom:link href="http://kevin.sb.org/tag/rspec/feed/" rel="self" type="application/rss+xml" />
	<link>http://kevin.sb.org</link>
	<description>The occasional view into my life</description>
	<lastBuildDate>Fri, 09 Sep 2011 00:19:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Guarding against `‌` and Kernel#system() in your tests</title>
		<link>http://kevin.sb.org/2008/05/14/guarding-against-shelling-out-in-your-tests/</link>
		<comments>http://kevin.sb.org/2008/05/14/guarding-against-shelling-out-in-your-tests/#comments</comments>
		<pubDate>Wed, 14 May 2008 08:35:34 +0000</pubDate>
		<dc:creator>Kevin Ballard</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://kevin.sb.org/?p=154</guid>
		<description><![CDATA[Update: I've refactored the code significantly and introduced the ability to explicitly unguard methods from within specs. One problem I find myself having when writing tests is ensuring that no execution ever leaves my little test sandbox. For example, when writing tests for github-gem I keep looking through the code to find every place a [...]]]></description>
			<content:encoded><![CDATA[<p><b>Update:</b> I've refactored the code significantly and introduced the ability to explicitly unguard methods from within specs.</p>
<p>One problem I find myself having when writing tests is ensuring that no execution ever leaves my little test sandbox. For example, when writing tests for <a href="http://github.com/kballard/github-gem">github-gem</a> I keep looking through the code to find every place a function ends up `shelling out` and it's tiresome and error-prone. I finally decided I was going about this backwards. Testing is supposed to find problems for me, right? So why not raise an error whenever `` or Kernel#system() is called and let the tests tell me what else I need to mock?</p>
<p>Put the following code in your spec_helper.rb file and an exception will be raised whenever `` or Kernel#system() is called during a test:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#9966CC; font-weight:bold;">Module</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> metaclass
    <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>;self;end
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> <span style="color:#6666ff; font-weight:bold;">Spec::Example::ExampleGroupSubclassMethods</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> add_guard<span style="color:#006600; font-weight:bold;">&#40;</span>klass, name, is_class = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    guarded = <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#008000; font-style:italic;"># define variable now for scoping</span>
    target = <span style="color:#006600; font-weight:bold;">&#40;</span>is_class ? klass.<span style="color:#9900CC;">metaclass</span> : klass<span style="color:#006600; font-weight:bold;">&#41;</span>
    sep = <span style="color:#006600; font-weight:bold;">&#40;</span>is_class ? <span style="color:#996600;">&quot;.&quot;</span> : <span style="color:#996600;">&quot;#&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    target.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      guarded = instance_method<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span>
      define_method name <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|*</span>args<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Testing guards violated: Cannot call #{klass}#{sep}#{name}&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0066ff; font-weight:bold;">@guards</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0066ff; font-weight:bold;">@guards</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>klass, name, is_class, guarded<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> add_class_guard<span style="color:#006600; font-weight:bold;">&#40;</span>klass, name<span style="color:#006600; font-weight:bold;">&#41;</span>
    add_guard<span style="color:#006600; font-weight:bold;">&#40;</span>klass, name, <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> unguard<span style="color:#006600; font-weight:bold;">&#40;</span>klass, name, is_class = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    row = <span style="color:#0066ff; font-weight:bold;">@guards</span>.<span style="color:#9900CC;">find</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span><span style="color:#006600; font-weight:bold;">&#40;</span>k,n,i<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">|</span> k == klass <span style="color:#9966CC; font-weight:bold;">and</span> n == name <span style="color:#9966CC; font-weight:bold;">and</span> i == is_class <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;#{klass}#{is_class ? '.' : '#'}#{name} is not guarded&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> row.<span style="color:#0000FF; font-weight:bold;">nil</span>?
    <span style="color:#006600; font-weight:bold;">&#40;</span>is_class ? klass.<span style="color:#9900CC;">metaclass</span> : klass<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      define_method name, row.<span style="color:#9900CC;">last</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0066ff; font-weight:bold;">@guards</span>.<span style="color:#9900CC;">delete</span> row
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> class_unguard<span style="color:#006600; font-weight:bold;">&#40;</span>klass, name<span style="color:#006600; font-weight:bold;">&#41;</span>
    unguard<span style="color:#006600; font-weight:bold;">&#40;</span>klass, name, <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> unguard_all
    <span style="color:#0066ff; font-weight:bold;">@guards</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0066ff; font-weight:bold;">@guards</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>klass, name, is_class, guarded<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#006600; font-weight:bold;">&#40;</span>is_class ? klass.<span style="color:#9900CC;">metaclass</span> : klass<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">class_eval</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        define_method name, guarded
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0066ff; font-weight:bold;">@guards</span>.<span style="color:#9900CC;">clear</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">Spec::Runner</span>.<span style="color:#9900CC;">configure</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>configuration<span style="color:#006600; font-weight:bold;">|</span>
  configuration.<span style="color:#9900CC;">prepend_before</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">send</span> :<span style="color:#9966CC; font-weight:bold;">include</span>, <span style="color:#6666ff; font-weight:bold;">Spec::Example::ExampleGroupSubclassMethods</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  configuration.<span style="color:#9900CC;">prepend_before</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    add_guard <span style="color:#CC00FF; font-weight:bold;">Kernel</span>, :<span style="color:#996600;">'`'</span>
    add_guard <span style="color:#CC00FF; font-weight:bold;">Kernel</span>, :<span style="color:#CC0066; font-weight:bold;">system</span>
    add_class_guard <span style="color:#CC00FF; font-weight:bold;">Process</span>, :<span style="color:#CC0066; font-weight:bold;">fork</span>
    add_class_guard Open3, <span style="color:#ff3333; font-weight:bold;">:popen3</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>? Open3
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  configuration.<span style="color:#9900CC;">append_after</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    unguard_all
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Note: if you want, you can write <code style="color:#996600">:`</code> instead of <code style="color:#996600">:'`'</code>, but my syntax highlighter here doesn't like that.</p>
]]></content:encoded>
			<wfw:commentRss>http://kevin.sb.org/2008/05/14/guarding-against-shelling-out-in-your-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
