<?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>Brain Matters &#187; mocking</title>
	<atom:link href="http://blog.agoragames.com/tag/mocking/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agoragames.com</link>
	<description></description>
	<lastBuildDate>Mon, 16 Aug 2010 19:36:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mocking HTTP request and response with Python and Mox</title>
		<link>http://blog.agoragames.com/2009/10/01/mocking-http-request-and-response-with-python-and-mox/</link>
		<comments>http://blog.agoragames.com/2009/10/01/mocking-http-request-and-response-with-python-and-mox/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 13:55:56 +0000</pubDate>
		<dc:creator>David Czarnecki</dc:creator>
				<category><![CDATA[Engineering]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[mox]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[urllib2]]></category>

		<guid isPermaLink="false">http://blog.agoragames.com/?p=368</guid>
		<description><![CDATA[I recently started integrating with some web services in Python and wanted to be able to test all the requests and responses with valid and invalid data. However, I need to be able to do this without actually hitting the web service when testing. And of course I don&#8217;t want to change my web service [...]]]></description>
			<content:encoded><![CDATA[<p>I recently started integrating with some web services in Python and wanted to be able to test all the requests and responses with valid and invalid data. However, I need to be able to do this without actually hitting the web service when testing. And of course I don&#8217;t want to change my web service code to conditionally do things if running in a test environment. Enter mocking and Mox.</p>
<p>One of our engineers, Tim, has written an excellent post on his adventures as a Ruby programmer getting up to speed on <a href="http://blog.agoragames.com/2009/02/23/python-is-mocking-me-a-ruby-programmers-adventures-with-pythonmox/">testing and mocking in Python using Mox</a>. You should read it.</p>
<p>Back to my issue with mocking the HTTP request and response.</p>
<p>Here is what I came up with. I first created a MockResponse class that adds a read() method to the string class.</p>
<p><code>
<pre class="brush: python">&lt;/code&gt;

class MockResponse(str):
&#039;&#039;&#039;
Mock response class with a method called read which will be used similar to the response from an HTTP request to a URL
&#039;&#039;&#039;
def read(self):
return self
</pre>
<p>And here is a test in which I mock out the request and response for the urllib2.OpenerDirector.open method. I don't so much care what the request string is passed to the open call, so I use IgnoreArg(). And I return the XML that would be returned by the actual web service to indicate the username and/or password were incorrect.</p>
<pre class="brush: python">

def test_logging_in_to_network_with_bad_username_and_password(self):
my_network_service = MyNetworkService(&#039;foo&#039;, &#039;bar&#039;)
self.mock(urllib2.OpenerDirector, &#039;open&#039;)
urllib2.OpenerDirector.open(mox.IgnoreArg()).AndReturn(MockResponse(&#039;&#039;&#039;
&lt; ?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;login&gt;
&lt;status&gt;
&lt;id&gt;30000&lt;/id&gt;
&lt;message&gt;ACCT_LOGIN_FAILED&lt;/message&gt;
&lt;/status&gt;
&lt;accountid&gt;7&lt;/accountid&gt;
&lt;/login&gt;
&#039;&#039;&#039;))
self.replay_all()

self.assertEqual(False, my_network_service.login())
self.mox.UnsetStubs()
self.mox.VerifyAll()
</pre>
<p>If there's a better way, I'd love to hear your suggestions.</p>




	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fblog.agoragames.com%2F2009%2F10%2F01%2Fmocking-http-request-and-response-with-python-and-mox%2F&amp;title=Mocking%20HTTP%20request%20and%20response%20with%20Python%20and%20Mox" title="Reddit"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fblog.agoragames.com%2F2009%2F10%2F01%2Fmocking-http-request-and-response-with-python-and-mox%2F&amp;title=Mocking%20HTTP%20request%20and%20response%20with%20Python%20and%20Mox&amp;bodytext=I%20recently%20started%20integrating%20with%20some%20web%20services%20in%20Python%20and%20wanted%20to%20be%20able%20to%20test%20all%20the%20requests%20and%20responses%20with%20valid%20and%20invalid%20data.%20However%2C%20I%20need%20to%20be%20able%20to%20do%20this%20without%20actually%20hitting%20the%20web%20service%20when%20testing.%20And" title="Digg"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fblog.agoragames.com%2F2009%2F10%2F01%2Fmocking-http-request-and-response-with-python-and-mox%2F&amp;title=Mocking%20HTTP%20request%20and%20response%20with%20Python%20and%20Mox&amp;notes=I%20recently%20started%20integrating%20with%20some%20web%20services%20in%20Python%20and%20wanted%20to%20be%20able%20to%20test%20all%20the%20requests%20and%20responses%20with%20valid%20and%20invalid%20data.%20However%2C%20I%20need%20to%20be%20able%20to%20do%20this%20without%20actually%20hitting%20the%20web%20service%20when%20testing.%20And" title="del.icio.us"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fblog.agoragames.com%2F2009%2F10%2F01%2Fmocking-http-request-and-response-with-python-and-mox%2F&amp;t=Mocking%20HTTP%20request%20and%20response%20with%20Python%20and%20Mox" title="Facebook"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.agoragames.com%2F2009%2F10%2F01%2Fmocking-http-request-and-response-with-python-and-mox%2F&amp;t=Mocking%20HTTP%20request%20and%20response%20with%20Python%20and%20Mox&amp;s=I%20recently%20started%20integrating%20with%20some%20web%20services%20in%20Python%20and%20wanted%20to%20be%20able%20to%20test%20all%20the%20requests%20and%20responses%20with%20valid%20and%20invalid%20data.%20However%2C%20I%20need%20to%20be%20able%20to%20do%20this%20without%20actually%20hitting%20the%20web%20service%20when%20testing.%20And" title="Tumblr"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/tumblr.png" title="Tumblr" alt="Tumblr" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Mocking%20HTTP%20request%20and%20response%20with%20Python%20and%20Mox%20-%20http%3A%2F%2Fblog.agoragames.com%2F2009%2F10%2F01%2Fmocking-http-request-and-response-with-python-and-mox%2F" title="Twitter"><img src="http://blog.agoragames.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://blog.agoragames.com/2009/10/01/mocking-http-request-and-response-with-python-and-mox/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! -->