<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Wojno: Tag scenario</title>
    <link>http://christopher.wojno.com/articles/tag/scenario</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Exploration through Code</description>
    <item>
      <title>Rails Unit Testing Fixtures: Scenarios</title>
      <description>&lt;p&gt;I hurriedly wrote an &lt;a href="/articles/2008/05/15/flexible-fixtures-in-rails-unit-tests"&gt;article this morning&lt;/a&gt; about my adaptation of fixtures I call sub-fixtures. While it seemed like a good idea in theory, it it beastly to implement in practice. It requires writing new fixtures for every unique test (if used correctly). When used incorrectly, the test names don&amp;#8217;t describe the data configuration being tested against.&lt;/p&gt;


	&lt;p&gt;I sat down and thought about it a little longer. What I really need is a scenario. A database state poised for a particular test. Needless to say: such a scenario is not limited to a single table. That way, the scenario is describing the database state being tested against and can therefore be used logically in other tests as well.&lt;/p&gt;


	&lt;p&gt;Only one minor change is required. In the test directory, add the following code to the body of the Test::Unit::TestCase class in test_helper.rb:&lt;/p&gt;


&lt;pre class="code"&gt;    def load_scenario( scenario )
        directory = File.dirname(__FILE__)+'/fixtures/scenarios/'+scenario+'/'
        files = Dir.entries( directory )
        files.delete( '.' ); files.delete( '..' )
        exp = /[^\.].*\.(yml|csv)$/
        models = files.select{|f| f[exp] }
        models = models.collect{|m| m.slice(0,m.length-4).to_sym}
        Fixtures.create_fixtures( directory, models )
    end&lt;/pre&gt;

	&lt;p&gt;Again, it requires a special directory structure (maybe I&amp;#8217;ll write a scenario generator later). in the test/fixtures I added a new directory: scenarios. Within that directory are other directories containing the scenarios fixture setups. For example, if I want to create a scenario where my list of weekly cookie recipes is missing a cookie and I want to test my code to see if it finds the correct missing cookie, I simply create the following directory:&lt;/p&gt;


&lt;pre&gt;test/fixtures/scenarios/missing_cookie&lt;/pre&gt;

	&lt;p&gt;And in that directory, I create the following fixture files:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;cookies.yml&lt;/li&gt;
		&lt;li&gt;users.yml&lt;/li&gt;
		&lt;li&gt;user_cookies.yml&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Note: it works for .csv files as well. Simply create the fixtures like you normally would do.&lt;/p&gt;


	&lt;p&gt;Now, in your unit test code, add the following line to the &lt;strong&gt;beginning&lt;/strong&gt; of your test case method:&lt;/p&gt;


&lt;pre class="code"&gt;load_scenario( 'missing_cookie' )&lt;/pre&gt;

	&lt;p&gt;Now, when your test runs, it will create the scenario in the database that you specify. It will automatically load all the fixture files in the directory.&lt;/p&gt;


	&lt;h1&gt;Problems&lt;/h1&gt;


	&lt;p&gt;Again, if you use normal fixtures, they will be loaded and unloaded (will make testing a little slower).&lt;/p&gt;</description>
      <pubDate>Thu, 15 May 2008 17:56:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:30c35008-c1f4-4fe9-97de-a98c3c5130af</guid>
      <author>Christopher Wojno</author>
      <link>http://christopher.wojno.com/articles/2008/05/15/rails-unit-testing-fixtures-scenarios</link>
      <category>Rails Snippets</category>
      <category>unit</category>
      <category>fixture</category>
      <category>scenario</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
