<?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>Workin' on it. &#187; Test Driven Development (TDD)</title>
	<atom:link href="http://jameshalberg.com/category/test-driven-development-tdd/feed/" rel="self" type="application/rss+xml" />
	<link>http://jameshalberg.com</link>
	<description></description>
	<lastBuildDate>Mon, 01 Mar 2010 03:02:37 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='jameshalberg.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/d6a237c3e92002b1021b5a1e47cc6c99?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>Workin' on it. &#187; Test Driven Development (TDD)</title>
		<link>http://jameshalberg.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jameshalberg.com/osd.xml" title="Workin&#8217; on it." />
	<atom:link rel='hub' href='http://jameshalberg.com/?pushpress=hub'/>
		<item>
		<title>MySQL Triggers w/Rails</title>
		<link>http://jameshalberg.com/2010/02/28/mysql-triggers-wrails/</link>
		<comments>http://jameshalberg.com/2010/02/28/mysql-triggers-wrails/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 02:54:24 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Test Driven Development (TDD)]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[triggers]]></category>

		<guid isPermaLink="false">http://jameshalberg.com/?p=391</guid>
		<description><![CDATA[I recently incorporated db-triggers with a Rails app to maintain some counts that were otherwise fairly expensive to retrieve.  Rails wasn&#8217;t super-pumped about the idea (what with the &#8220;keep all the logic in the app&#8221; approach and all), but sometimes&#8230; you know&#8230; you know better than your framework.
Some things I was aiming for:

Set them up with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=391&subd=jameshalberg&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I recently incorporated db-triggers with a Rails app to maintain some counts that were otherwise fairly expensive to retrieve.  Rails wasn&#8217;t super-pumped about the idea (what with the &#8220;keep all the logic in the app&#8221; approach and all), but sometimes&#8230; you know&#8230; you know better than your framework.</p>
<p>Some things I was aiming for:</p>
<ol>
<li>Set them up with normal migrations.</li>
<li>Test them with the normal test suite/normal fixtures.</li>
<li>Make recovery/reset simple for when the table (inevitably) is somehow out of sync.</li>
</ol>
<p><strong>The &#8220;frequent counts&#8221; table</strong></p>
<p>I&#8217;ll have multiple counts but not TOO many &#8212; enough that I don&#8217;t want to have a column per count but not enough that I mind using &#8220;LIKE&#8221; to lookup patterns, so my table has: id, code, current_count.</p>
<p>Code will be a unique key (important later) and be formatted like &#8220;style_ABC_size_456&#8243;.</p>
<p>So, when a new item is added it&#8217;ll be associated with a style and some sizes &#8211; each combination will either need to be setup (with a current_count = 1) or an existing combo will be found and +=1.</p>
<p>The FrequentCount class has the fairly straightforward finders that you&#8217;d expect + methods to reset each of the counts that it contains.  The reset methods follow the pattern &#8220;reset_frequent_count_COUNT_NAME&#8221; -&gt; they clear the existing counts that they maintain before repopulating them.</p>
<p>I also threw in a reset_all method that looks for anything on the class following the &#8220;reset_frequent_count_COUNT_NAME&#8221; pattern and runs them.</p>
<p><strong>The trigger-SQL</strong></p>
<p>The SQL for creating the triggers will be needed by the migration as well as the test suite.  In fact, the test suite will need to run them somewhat often due to the way the standard tests &#8221;prepare&#8221; the database.</p>
<p>I ended up throwing it in lib/trigger_sql.rb.  Methods there are named with the pattern &#8220;sql_for_TABLE_OPERATION_TRIGGER_NAME&#8221; ex: sql_for_items_insert_style_and_size</p>
<div id="_mcePaste">Many of the triggers could not rely on pre-existing rows.  i.e. a new style/size combination needs to INSERT where an existing combo could update ( +=1 ).  To get around this, I relied on the unique key setup earlier on the &#8220;code&#8221; field for the frequent counts table.  &lt;&#8211; that allowed me to lean on insert statements with &#8220;ON DUPLICATE KEY&#8221; clauses with update statements.  Something like this&#8230;</div>
<blockquote style="margin:25px 0;">
<div>create trigger items_insert_style_and_size after insert on items<br />
for each row<br />
begin<br />
insert into frequent_counts(code, current_count)<br />
values (concat(&#8217;style_&#8217;, new.style, &#8216;_size_&#8217;, new.size), 1)<br />
on duplicate key update current_count = current_count + 1;<br />
end;</div>
</blockquote>
<div><strong><span style="font-weight:normal;"><strong>The Migration</strong></p>
<div id="_mcePaste">I&#8217;ve already given away most of the fun stuff about the migration.  It just needs to run through the triggers that are being setup at this specific time, doing things like:</div>
<blockquote style='margin:25px 0;'>
<div>TriggerSql.connection.execute(TriggerSql.sql_for_items_insert_items_by_style_and_size)</div>
</blockquote>
<div>and then make sure to populate it all (with that reset_all) method when we&#8217;re done. &lt;&#8211; next time out I may want to call specific methods to reset just the ones I care about but this first time, I can just do the whole table.</div>
<div></div>
<p></span></strong></div>
<div><strong>Testing with Fixtures</strong></div>
<div>Rails goes a little too far when running the default test tasks for us &#8211; it ends up nuking the triggers on us, but not to fear: it&#8217;s a quick hack in the Rakefile.</div>
<div></div>
<div>I&#8217;m going to spare you some details (drop me a line if you want them) but I basically overrode the db:test:prepare method to call a special version of the clone_structure task.  My version has a dependent task that does:</div>
<blockquote style='margin:25px 0;'>
<div>
<div># find methods that follow our pattern of &#8220;methods providing trigger sql&#8221; and execute the contents of each</div>
<div>TriggerSql.methods.select{ |m| m =~ /sql_for_.+/ }.each do |method_name|</div>
<div>ActiveRecord::Base.connection.execute(TriggerSql.send(method_name))</div>
<div>end</div>
</div>
</blockquote>
<div>As you see there, it&#8217;s leaning on that naming convention &#8220;sql_for_TABLE_OPERATION_TRIGGER_NAME&#8221; to find the sql to (re)apply.</div>
<div></div>
<div style='margin-top:10px;'><strong>That&#8217;s it!</strong></div>
<div>Migrations set them up and share the code to do so with the fixtures that can repeat the tests whenever we need.  Those reset methods also come in handy not only for the initial population (by the migration) but we can call them manually should we need them.</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshalberg.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshalberg.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshalberg.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshalberg.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshalberg.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshalberg.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshalberg.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshalberg.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshalberg.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshalberg.wordpress.com/391/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=391&subd=jameshalberg&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://jameshalberg.com/2010/02/28/mysql-triggers-wrails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jameshalberg</media:title>
		</media:content>
	</item>
		<item>
		<title>Receiving Email with ActionMailer</title>
		<link>http://jameshalberg.com/2006/12/06/receiving-email-with-actionmailer/</link>
		<comments>http://jameshalberg.com/2006/12/06/receiving-email-with-actionmailer/#comments</comments>
		<pubDate>Wed, 06 Dec 2006 10:31:19 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Email]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Test Driven Development (TDD)]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://jameshalberg.wordpress.com/2006/12/06/receiving-email-with-actionmailer/</guid>
		<description><![CDATA[This really was a snap.
A nice and simple testing recipe (#68) demonstrates how to read in an email from a fixture in just a few lines and pass them to your processing method (MailReceiver.receive in this case).
def read_fixture(action)
IO.readlines(&#8220;#{FIXTURES_PATH}/mail_receiver/#{action}&#8221;)
end
def test_something
email = read_fixture(&#8220;junk_mail&#8221;).join
MailReceiver.receive(email)
# assertions
end
And then a few lines on how to feed it &#8220;for real&#8221;.  and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=60&subd=jameshalberg&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>This really was a snap.</p>
<p>A nice and simple <a href="http://www.pragmaticprogrammer.com/titles/fr_rr/">testing recipe</a> (#68) demonstrates how to read in an email from a fixture in just a few lines and pass them to your processing method (MailReceiver.receive in this case).</p>
<p><em>def read_fixture(action)<br />
IO.readlines(&#8220;#{FIXTURES_PATH}/mail_receiver/#{action}&#8221;)<br />
end</em></p>
<p><em>def test_something<br />
email = read_fixture(&#8220;junk_mail&#8221;).join<br />
MailReceiver.receive(email)</em><br />
<em># assertions<br />
end</em></p>
<p>And then a few lines on how to feed it <a href="http://wiki.rubyonrails.org/rails/pages/HowToReceiveEmailsWithActionMailer">&#8220;for real&#8221;</a>.  and we&#8217;re up and running!</p>
<p>The only catch&#8230; there was some anger over the parens:</p>
<pre>mailman: "|/path/to/app/script/runner Mailman.receive(STDIN.read)"</pre>
<p>We ended up escaping them, but according to those directions I just linked to &#8211; you can also just quote the call to receive.</p>
<pre></pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jameshalberg.wordpress.com/60/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jameshalberg.wordpress.com/60/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshalberg.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshalberg.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshalberg.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshalberg.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshalberg.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshalberg.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshalberg.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshalberg.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshalberg.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshalberg.wordpress.com/60/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=60&subd=jameshalberg&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://jameshalberg.com/2006/12/06/receiving-email-with-actionmailer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jameshalberg</media:title>
		</media:content>
	</item>
		<item>
		<title>Be careful with assert_not_equal</title>
		<link>http://jameshalberg.com/2006/10/02/be-careful-with-assert_not_equal/</link>
		<comments>http://jameshalberg.com/2006/10/02/be-careful-with-assert_not_equal/#comments</comments>
		<pubDate>Mon, 02 Oct 2006 21:40:41 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Test Driven Development (TDD)]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://jameshalberg.wordpress.com/2006/10/02/be-careful-with-assert_not_equal/</guid>
		<description><![CDATA[As I discovered today, assert_not_equal has a pretty big problem: it does exactly what you ask it to do.
I just need to make sure that a user has at least one record:
assert_not_equal 0, UserPreference.find_by_user_id(user_id)
As you may have just caught: that is always going to pass.  Not a tough mistake when you&#8217;re motoring along and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=57&subd=jameshalberg&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>As I discovered today, assert_not_equal has a pretty big problem: it does exactly what you ask it to do.</p>
<p>I just need to make sure that a user has at least one record:</p>
<p><em>assert_not_equal 0, UserPreference.find_by_user_id(user_id)</em></p>
<p>As you may have just caught: that is always going to pass.  Not a tough mistake when you&#8217;re motoring along and not putting too much thought into something so simple:  The finder isn&#8217;t going to return the count of matching rows&#8230; it&#8217;s either going to return a populated object or nil, neither of which will be equal to 0.</p>
<p>Fixing the bug is trivial, but lesson learned:</p>
<p>From now on whenever I write assert_not_equal &#8211; I am going to make sure it works by temporarily changing it to assert_equal and taking a look at the error message.</p>
<p>In this case: if the error message said something like &#8220;&lt;0&gt; expected but was &lt;2&gt;&#8221;, I would know that things are working.</p>
<p>Whereas if the error said &#8220;&lt;0&gt; expected but was &lt;#&lt;UserPreference&#8230;&gt;&gt;&#8221;, I&#8217;d know right away that I need to fix it up (and have one more cup of coffee).</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jameshalberg.wordpress.com/57/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jameshalberg.wordpress.com/57/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshalberg.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshalberg.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshalberg.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshalberg.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshalberg.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshalberg.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshalberg.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshalberg.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshalberg.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshalberg.wordpress.com/57/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=57&subd=jameshalberg&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://jameshalberg.com/2006/10/02/be-careful-with-assert_not_equal/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jameshalberg</media:title>
		</media:content>
	</item>
		<item>
		<title>Adding tests to rake</title>
		<link>http://jameshalberg.com/2006/07/14/adding-tests-to-rake/</link>
		<comments>http://jameshalberg.com/2006/07/14/adding-tests-to-rake/#comments</comments>
		<pubDate>Sat, 15 Jul 2006 01:17:45 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Test Driven Development (TDD)]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Rake]]></category>

		<guid isPermaLink="false">https://jameshalberg.wordpress.com/2006/07/14/adding-tests-to-rake/</guid>
		<description><![CDATA[We&#8217;ve developed a few batch applications that live in the same directory structure as the Rails app they&#8217;re intimate with.  So, the lib/foo/test/ contains some tests for the &#8216;foo&#8217; batch app.  lib/bar/test has the ones for &#8216;bar&#8217; etc.
It was kind of annoying to have seperate rake files though &#8211; or worse to need [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=53&subd=jameshalberg&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve developed a few batch applications that live in the same directory structure as the Rails app they&#8217;re intimate with.  So, the lib/foo/test/ contains some tests for the &#8216;foo&#8217; batch app.  lib/bar/test has the ones for &#8216;bar&#8217; etc.</p>
<p>It was kind of annoying to have seperate rake files though &#8211; or worse to need to execute each of the batch unit tests seperately.</p>
<p>Since the number of tests required is quite small at this point &#8211; they all just live directly under the test dir (not broken out into unit, functional, etc)&#8230;</p>
<blockquote><p>desc &#8216;Test the batch stuff.&#8217;<br />
Rake::TestTask.new(:test) do |t|</p>
<p>t.pattern = &#8216;lib/**/test/*_test.rb&#8217;<br />
t.verbose = true</p>
<p>end</p></blockquote>
<p>Nice and easy <b>again</b>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jameshalberg.wordpress.com/53/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jameshalberg.wordpress.com/53/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshalberg.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshalberg.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshalberg.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshalberg.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshalberg.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshalberg.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshalberg.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshalberg.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshalberg.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshalberg.wordpress.com/53/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=53&subd=jameshalberg&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://jameshalberg.com/2006/07/14/adding-tests-to-rake/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jameshalberg</media:title>
		</media:content>
	</item>
		<item>
		<title>No help from the log</title>
		<link>http://jameshalberg.com/2006/05/31/no-help-from-the-log/</link>
		<comments>http://jameshalberg.com/2006/05/31/no-help-from-the-log/#comments</comments>
		<pubDate>Wed, 31 May 2006 22:28:10 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Test Driven Development (TDD)]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">https://jameshalberg.wordpress.com/2006/05/31/no-help-from-the-log/</guid>
		<description><![CDATA[Upon synching up, a colleague noticed that I had a test failing&#8230; kinda odd since I&#39;ve been (trying to be) pretty good about keeping things up to date.  Running them myself, we noticed that I was actually experiencing a quiet error message that was preventing my unit tests from running at all.  I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=45&subd=jameshalberg&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Upon synching up, a colleague noticed that I had a test failing&#8230; kinda odd since I&#39;ve been (trying to be) pretty good about keeping things up to date.  Running them myself, we noticed that I was actually experiencing a quiet error message that was preventing my unit tests from running at all.  I had been seeing the familiar &quot;0 failures, 0 errors&quot; message for the functional tests and not really noticing that there was no message at all for my unit tests.</p>
<p>Running a trace (and paying attention now <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) it seemed to be experiencing an error executing the integration tests.</p>
<p>The helpful error message? &#8230; rake aborted!</p>
<p>Can you expand on that? &#8230; Test failures</p>
<p>oh, thanks!</p>
<p>After a good hour of banging my head against the wall I modified the rakefile&#39;s default task:<br />
task :default =&gt; [:test_units, :test_functional]</p>
<p>Now, the rerun actually gave me a decent error!<br />
Mysql::Error: Table &#39;tags&#39; already exists: CREATE TABLE tags (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `code` int(11), `name` varchar(255)) ENGINE=InnoDB</p>
<p>10 seconds later I had dropped the table &#8211; let the rake recreate it itself &#8211; and everything was fine!  Would have been nice if I didn&#39;t have to hold it&#39;s head underwater to get that information out of it!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jameshalberg.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jameshalberg.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshalberg.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshalberg.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshalberg.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshalberg.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshalberg.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshalberg.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshalberg.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshalberg.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshalberg.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshalberg.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=45&subd=jameshalberg&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://jameshalberg.com/2006/05/31/no-help-from-the-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jameshalberg</media:title>
		</media:content>
	</item>
		<item>
		<title>Requiring and Testing Cookies</title>
		<link>http://jameshalberg.com/2006/05/12/requiring-and-testing-cookies/</link>
		<comments>http://jameshalberg.com/2006/05/12/requiring-and-testing-cookies/#comments</comments>
		<pubDate>Fri, 12 May 2006 20:53:12 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Cookies]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Test Driven Development (TDD)]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">https://jameshalberg.wordpress.com/2006/05/12/requiring-and-testing-cookies/</guid>
		<description><![CDATA[Requirement: Users must have cookies enabled to use the site.  If they don&#39;t: give them some sort of graceful warning to let them know there&#39;s going to be trouble.  Doesn&#39;t sound too bad&#8230; it ended up being much more of an adventure than I had expected.
Step 1. Add the cookie requirement
After a bit [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=32&subd=jameshalberg&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p><b>Requirement:</b> Users must have cookies enabled to use the site.  If they don&#39;t: give them some sort of graceful warning to let them know there&#39;s going to be trouble.  Doesn&#39;t sound too bad&#8230; it ended up being much more of an adventure than I had expected.</p>
<p><b>Step 1. Add the cookie requirement</b></p>
<p>After a bit of deliberation I decided that I would rather not use Javascript (if I didn&#39;t have to). The trouble with testing for cookies with _only_ server side code is&#8230; well, you are _only_ on the server side &#8211; and &#39;cookies enabled&#39; isn&#39;t exactly a server setting <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .  When setting a cookie value &#8211; you are merely setting it in a hash to be persisted later (when the request finishes processing) &#8211; therefore you won&#39;t really know if it is going to _work_ in the browser until&#8230; well&#8230; you actually try to set it in the browser.</p>
<p>As you likely know, rails uses a cookie by default to track the session id (cookie id is :_session_id).  So, it seems easy enough that I can verify that cookie is there to determine if cookies are enabled.</p>
<p>The catch, of course, is that on the first request: my code will check if the cookie is present&#8230; see that it isn&#39;t (the browser didn&#39;t _already_ have it) and conclude that cookies are disabled!  That&#39;s not going to work: in all likelihood they are enabled&#8230; it&#39;s just a matter of me not getting a chance to set the thing yet.</p>
<p>So, I&#39;m going to drop in a little workaround:</p>
<p>First check for the _session_id cookie.  If it&#39;s there: great, we&#39;re good to go.  Mission accomplished: proceed to the requested functionality with my blessings.<br />
If not, what do we know?  Not much.  We know that the cookie hasn&#39;t _already_ been added, but we don&#39;t know if that&#39;s because it&#39;s actually been blocked&#8230; or have we just not even attempted it yet?   What&#39;s the easiest way to determine if this is the first time the application has been accessed?  Personally, I think it&#39;s to not even try to figure it out and just make another call: when that call is fielded you _know_ it&#39;s not the first!</p>
<p>So, we add a &#39;special&#39; parameter and repeat the users request.  The subsequent request will be able to check not only for the presence of our cookie but also for the presence of our special parameter &#8211; if both turn up negative: we can conclude that cookies are disabled.</p>
<p><img src="http://jameshalberg.files.wordpress.com/2006/05/before_12345.png?w=500" alt="before_12345.png" /><br />
<b>Step 2. Add the unit tests</b></p>
<p>Alright, so all my functional tests are suddenly failing since they are all being blocked by the cookie requirement!  I need to add my simple test for the code I just added &#8211; while also modifying the existing tests to have proper credentials to access the app.  No prob, right?  I already have my tests jumping through a little hoop to properly &#39;login&#39;, so now in addition to the &quot;authenticate&quot; stuff I already have: I just add a piece of code to add my cookie:<br />
<img src="http://jameshalberg.files.wordpress.com/2006/05/first_cookie_test.png?w=500" alt="first_cookie_test.png" /></p>
<p>Run the tests&#8230; trouble. The cookie isn&#39;t being found. This does make some sense: what does &quot;cookies&quot; really mean to my test class? So, I change things so that instead of saying cookies[:_session_id], I say:</p>
<p><img src="http://jameshalberg.files.wordpress.com/2006/05/final_cookie_test.png?w=500" alt="final_cookie_test.png" /></p>
<p>Now this works for the test&#8230; but now it isn&#39;t working in the actual web app!</p>
<p><b>Step 3. Making it Work</b>I suppose I could drop in another piece of code to have my filter check both cookies and @request.cookies but that&#39;s no good.  I need to use @request.cookies for both test and &#39;application&#39; code.<br />
The final snags&#8230;</p>
<p>a.  @request.cookies won&#39;t play nice with my symbol ( :_session_id ), so I need to actually use a string for the key (&quot;_session_id&quot;).</p>
<p>b.  @request.cookies[&quot;_session_id&quot;] is returning a Hash and is also returning blank instead of null if not found</p>
<p>So&#8230; without further adu:</p>
<p>The test code:</p>
<p><img src="http://jameshalberg.files.wordpress.com/2006/05/final_cookie_test.png?w=500" alt="final_cookie_test.png" /></p>
<p>The app code:</p>
<p><img src="http://jameshalberg.files.wordpress.com/2006/05/after_12345.png?w=500" alt="after_12345.png" /></p>
<p>As with everyting Rails: the end solution is so simple it&#39;s painful.</p>
<p>And they lived happily ever after, The End.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jameshalberg.wordpress.com/32/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jameshalberg.wordpress.com/32/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshalberg.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshalberg.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshalberg.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshalberg.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshalberg.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshalberg.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshalberg.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshalberg.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshalberg.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshalberg.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=32&subd=jameshalberg&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://jameshalberg.com/2006/05/12/requiring-and-testing-cookies/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jameshalberg</media:title>
		</media:content>

		<media:content url="http://jameshalberg.files.wordpress.com/2006/05/before_12345.png" medium="image">
			<media:title type="html">before_12345.png</media:title>
		</media:content>

		<media:content url="http://jameshalberg.files.wordpress.com/2006/05/first_cookie_test.png" medium="image">
			<media:title type="html">first_cookie_test.png</media:title>
		</media:content>

		<media:content url="http://jameshalberg.files.wordpress.com/2006/05/final_cookie_test.png" medium="image">
			<media:title type="html">final_cookie_test.png</media:title>
		</media:content>

		<media:content url="http://jameshalberg.files.wordpress.com/2006/05/final_cookie_test.png" medium="image">
			<media:title type="html">final_cookie_test.png</media:title>
		</media:content>

		<media:content url="http://jameshalberg.files.wordpress.com/2006/05/after_12345.png" medium="image">
			<media:title type="html">after_12345.png</media:title>
		</media:content>
	</item>
		<item>
		<title>Testing the controller</title>
		<link>http://jameshalberg.com/2006/04/27/testing-the-controller/</link>
		<comments>http://jameshalberg.com/2006/04/27/testing-the-controller/#comments</comments>
		<pubDate>Fri, 28 Apr 2006 02:02:32 +0000</pubDate>
		<dc:creator>Jim</dc:creator>
				<category><![CDATA[Agile Software Dev]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Test Driven Development (TDD)]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">https://jameshalberg.wordpress.com/2006/04/27/testing-the-controller/</guid>
		<description><![CDATA[A couple things I found very handy today while setting up a functional test for a controller.
First off, this controller relies on a user being logged in (but this isn&#39;t the controller that actually manages the login process).
The easiest way I found to make sure that I was properly logged in was to temporarily change [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=28&subd=jameshalberg&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>A couple things I found very handy today while setting up a functional test for a controller.</p>
<p>First off, this controller relies on a user being logged in (but this isn&#39;t the controller that actually manages the login process).</p>
<p>The easiest way I found to make sure that I was properly logged in was to temporarily change @controller to the UserController &#8212; login &#8212; then change back.  Like this:</p>
<p><img src="http://jameshalberg.files.wordpress.com/2006/04/controllerswap.png?w=500" alt="controllerswap.png" /></p>
<p>A <a href="http://lists.rubyonrails.org/pipermail/rails/2005-March/004229.html">handy pointer</a> regarding testing instance variables from Jeremy Kemper.</p>
<p>To access an instance variable in the controller that you are testing: simply examine what would have been sent to the view, in the response:</p>
<p>something = @response.template.assigns[&#39;something&#39;]&nbsp;</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jameshalberg.wordpress.com/28/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jameshalberg.wordpress.com/28/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jameshalberg.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jameshalberg.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jameshalberg.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jameshalberg.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jameshalberg.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jameshalberg.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jameshalberg.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jameshalberg.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jameshalberg.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jameshalberg.wordpress.com/28/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jameshalberg.com&blog=160606&post=28&subd=jameshalberg&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://jameshalberg.com/2006/04/27/testing-the-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jameshalberg</media:title>
		</media:content>

		<media:content url="http://jameshalberg.files.wordpress.com/2006/04/controllerswap.png" medium="image">
			<media:title type="html">controllerswap.png</media:title>
		</media:content>
	</item>
	</channel>
</rss>