<?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>The Vose Way: Tag testing</title>
    <link>http://www.chuckvose.com/articles/tag/testing</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A Blog about Butter, Cheese, and Ruby on Rails</description>
    <item>
      <title>Testing rails without fixtures</title>
      <description>&lt;p&gt;At &lt;a href="http://www.create-on.com"&gt;On &amp;#38; On Creative&lt;/a&gt; we have a lot of sites on a common rails backend. It&amp;#8217;s incredibly flexible so we can offer a lot of support to customers but because there are so many customers with different needs testing has become a huge pain. Not only does it let us run tests custom to each client, it provides a way to import the data to the test db really easily and it&amp;#8217;s way faster than loading the fixtures each time you start a test. Yay!&lt;/p&gt;


	&lt;p&gt;After googling around I happened upon &lt;a href="http://blog.caboo.se/articles/2007/2/13/what-about-those-bloated-tests"&gt;this caboo.se article&lt;/a&gt; about how to run without fixtures at all. It&amp;#8217;s a very indepth article with lots of good things in it; far be it for me to try to compete.&lt;/p&gt;


	&lt;p&gt;Instead, here&amp;#8217;s the low down on how we prepare each of our sites for testing.&lt;/p&gt;


	&lt;p&gt;Add the following to the end of your rails_root Rakefile:&lt;/p&gt;


&lt;code&gt;
class Rake::Task
  def detract(prerequisite)
    @prerequisites.delete(prerequisite)
  end
end

%w(units functionals integration recent uncommitted).each do |task|
  Rake::Task["test:#{task}"].detract('db:test:prepare')
  Rake::Task["test:#{task}"].enhance(['environment'])
end
&lt;/code&gt;

	&lt;p&gt;Add the following to lib/tasks/fixtures.rake. Make sure to look at the two commented lines:&lt;/p&gt;


&lt;code&gt;
# This code courtesy of the "Rails Recipe's book":http://www.pragmaticprogrammer.com/titles/fr_rr/ I believe. 
namespace :db do
  namespace :fixtures do

    desc 'Create YAML test fixtures from data in an existing database. Defaults to development database. Set RAILS_ENV to override.'
    task :dump =&amp;gt; :environment do
      # Remove the limit if you want to. It's a timesaver mostly
      sql  = "SELECT * FROM %s LIMIT 0,1000" 
      # Add tables you don't want dumped. 
      skip_tables = ["schema_info", "plugin_schema_info", "engine_schema_info", "sessions"]
      ActiveRecord::Base.establish_connection(:development)
      (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
        i = "000" 
        File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|
          data = ActiveRecord::Base.connection.select_all(sql % table_name)
          file.write data.inject({}) { |hash, record|
            hash["#{table_name}_#{i.succ!}"] = record
            hash
          }.to_yaml
        end
      end
    end
  end
end
&lt;/code&gt;

	&lt;p&gt;Then the fun easy stuff happens:&lt;/p&gt;


&lt;code&gt;
rake db:fixtures:dump RAILS_ENV=production
rake db:fixtures:load RAILS_ENV=test
rake test:plugins PLUGIN=your_plugin
&lt;/code&gt;

	&lt;p&gt;If you want to commit the fixtures to the repo you can but it seems a waste to me since you might be regenerating them often.&lt;/p&gt;


	&lt;p&gt;Also, check the two comments in the fixtures.rake, it could be doing things you don&amp;#8217;t expect.&lt;/p&gt;</description>
      <pubDate>Wed, 25 Apr 2007 16:21:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:d02a27e0-93c9-4fc0-8819-8c41a2e2f22d</guid>
      <author>vosechu@create-on.com (Chuck Vose)</author>
      <link>http://www.chuckvose.com/articles/2007/04/25/testing-rails-without-fixtures</link>
      <category>Rails</category>
      <category>Programming</category>
      <category>Rails</category>
      <category>rake</category>
      <category>plugins</category>
      <category>fixtures</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
