<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>s-expressions &#187; testing</title>
	<atom:link href="http://s-expressions.com/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://s-expressions.com</link>
	<description>Amit Rathore blogs about software development</description>
	<lastBuildDate>Tue, 17 Jan 2012 17:58:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='s-expressions.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>s-expressions &#187; testing</title>
		<link>http://s-expressions.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://s-expressions.com/osd.xml" title="s-expressions" />
	<atom:link rel='hub' href='http://s-expressions.com/?pushpress=hub'/>
		<item>
		<title>conjure &#8211; simple mocking and stubbing for Clojure unit-tests</title>
		<link>http://s-expressions.com/2010/01/24/conjure-simple-mocking-and-stubbing-for-clojure-unit-tests/</link>
		<comments>http://s-expressions.com/2010/01/24/conjure-simple-mocking-and-stubbing-for-clojure-unit-tests/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 07:28:37 +0000</pubDate>
		<dc:creator>Amit Rathore</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://sexp.wordpress.com/?p=281</guid>
		<description><![CDATA[Siva and I were pairing on a unit-test that involved writing something to HBase. When Siva said that mocking the call to the save-to-hbase function would make testing easier (a simple thing using JMock, he said), I decided to write a quick mocking utility for Clojure. Then later, we realized that we wanted to go [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=281&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Siva and I were pairing on a unit-test that involved writing something to HBase. When Siva said that mocking the call to the <em>save-to-hbase</em> function would make testing easier (a simple thing using JMock, he said), I decided to write a quick mocking utility for Clojure. </p>
<p>Then later, we realized that we wanted to go one step further.  The row-id that was used as the key to the object in HBase was generated using system-time. That meant that even if we wanted to confirm that the object was indeed saved, we had no way of knowing what the row-id was. One solution to such a problem is to inject the row-id in (instead of being tightly coupled to the function that generated the row-id). Instead, I wrote a stubbing utility that makes this arbitrarily easy to do.</p>
<p>So here they are &#8211; <em>mocking</em> and <em>stubbing</em> &#8211; packaged up as the <a href="http://github.com/amitrathore/conjure">conjure</a> project on github.</p>
<h2>The set up</h2>
<p>Imagine we had the following functions -</p>
<p><pre class="brush: plain;">
(defn xx [a b]
  10)

(defn yy [z]
  20)

(defn fn-under-test []
  (xx 1 2)
  (yy  &quot;blah&quot;))

(defn another-fn-under-test []
  (+ (xx nil nil) (yy nil)))
</pre></p>
<p>Also imagine that we had to test <em>fn-under-test</em> and <em>another-fn-under-test</em>, and we didn&#8217;t want to have to deal with the <em>xx</em> or <em>yy</em> functions. Maybe they&#8217;re horrible functions that open connections to computers running Windoze or something, I dunno.</p>
<h2>Mocking</h2>
<p>Here&#8217;s how we might mock them out -</p>
<p><pre class="brush: plain;">
(deftest test-basic-mocking
  (mocking [xx yy]
    (fn-under-test))
  (verify-call-times-for xx 1)
  (verify-call-times-for yy 1)
  (verify-first-call-args-for xx 1 2)
  (verify-first-call-args-for yy &quot;blah&quot;))
</pre></p>
<p>Pretty straightforward, eh? You just use the <em>mocking</em> macro, specifying all the functions that need to be mocked out. Then, within the scope of <em>mocking</em>, you call your functions that need to be tested. The calls to the specified functions will get mocked out (they won&#8217;t occur), and you can then use things like <em>verify-call-times-for</em> and <em>verify-first-call-args-for</em> to ensure things worked as expected.</p>
<h2>Stubbing</h2>
<p>As mentioned in the intro to this post, sometimes your tests need to specify values to be returned by the functions being mocked out. That&#8217;s where <em>stubbing</em> comes in. Here&#8217;s how it works -</p>
<p><pre class="brush: plain;">
(deftest test-basic-stubbing
  (is (= (another-fn-under-test) 30))
  (stubbing [xx 1 yy 2]
    (is (= (another-fn-under-test) 3))))
</pre></p>
<p>So that&#8217;s it! Pretty simple. Note how within the scope of <em>stubbing</em>, <em>xx</em> returns 1 and <em>yy</em> returns 2. Now, for the implementation.</p>
<h2>Implementation</h2>
<p>The code is almost embarrassingly straight-forward. Take a look -</p>
<p><pre class="brush: plain;">
(ns org.rathore.amit.conjure.core
  (:use clojure.test))

(def call-times (atom {}))

(defn stub-fn [function-name return-value]
  (swap! call-times assoc function-name [])
  (fn [&amp; args]
    (swap! call-times update-in [function-name] conj args)
    return-value))

(defn mock-fn [function-name]
  (stub-fn function-name nil))

(defn verify-call-times-for [fn-name number]
  (is (= number (count (@call-times fn-name)))))

(defn verify-first-call-args-for [fn-name &amp;amp;amp; args]
  (is (= args (first (@call-times fn-name)))))

(defn verify-nth-call-args-for [n fn-name &amp;amp;amp; args]
  (is (= args (nth (@call-times fn-name) (dec n)))))

(defn clear-calls []
  (reset! call-times {}))

(defmacro mocking [fn-names &amp;amp;amp; body]
  (let [mocks (map #(list 'mock-fn %) fn-names)]
    `(binding [~@(interleave fn-names mocks)]
       ~@body)))

(defmacro stubbing [stub-forms &amp;amp;amp; body]
  (let [stub-pairs (partition 2 stub-forms)
        fn-names (map first stub-pairs)
        stubs (map #(list 'stub-fn (first %) (last %)) stub-pairs)]
    `(binding [~@(interleave fn-names stubs)]
       ~@body)))
</pre></p>
<p>It&#8217;s just an hour or so of work, so it&#8217;s probably rough, and certainly doesn&#8217;t support more complex features of other mocking/stubbing libraries. But I thought the simplicity was enjoyable.</p>
<br />Posted in Uncategorized  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sexp.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sexp.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sexp.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sexp.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sexp.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sexp.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sexp.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sexp.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sexp.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sexp.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sexp.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sexp.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sexp.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sexp.wordpress.com/281/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=281&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://s-expressions.com/2010/01/24/conjure-simple-mocking-and-stubbing-for-clojure-unit-tests/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/69c7419d10661aa2ab01de450a0127b2?s=96&#38;d=monsterid" medium="image">
			<media:title type="html">Amit Rathore</media:title>
		</media:content>
	</item>
	</channel>
</rss>
