<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Life without fixtures Changes</title>
  <id>tag:opensoul.org,2009:/2008/8/21/life-without-fixtures/changes</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://opensoul.org/2008/8/21/life-without-fixtures/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
  <updated>2008-08-21T02:12:35Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:471:1</id>
    <updated>2008-08-21T02:12:35Z</updated>
    <link href="http://opensoul.org/2009/3/8/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Life without fixtures</title>
<content type="html">&lt;p&gt;All the cool kids tell me that fixtures just aren&#8217;t the &#8220;in&#8221; thing anymore. Speaking of which, when did fanny-packs stop being cool? I wish someone would have said something…&lt;/p&gt;


	&lt;p&gt;Anyway, while I still haven&#8217;t fallen out of love with (foxy) fixtures, I have taken some of the home-grown alternative methods for a spin, but so far, I&#8217;ve been slightly frustrated with my experience.&lt;/p&gt;


	&lt;p&gt;The predominant replacement seems to be the &#8220;factory&#8221; pattern, talked about by &lt;a href=&quot;http://www.dcmanges.com/blog/38&quot;&gt;Dan Manges&lt;/a&gt; and then followed up by the &lt;a href=&quot;http://replacefixtures.rubyforge.org/&quot;&gt;FixtureReplacement&lt;/a&gt; plugin. The basic idea is that all your tests/specs should setup the data that they require, and there is a factory that makes it easy to create the necessary valid test data. For example, a homegrown solution might look like this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Generate
  def self.person(options={})
    p = Person.create!({
      :name     =&amp;gt; 'Brandon',
      :login    =&amp;gt; 'brandon',
      :password =&amp;gt; 'testing'
    }.merge(options))
    p.account ||= Generate.account
  end
end

describe Person, '#authenticate' do
  it 'should return the person record if successful' do
    person = Generate.person
    Person.authenticate(person.login, 'testing').should == person
  end
end&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This method scales pretty well. There are some issues, such as the use of &lt;code&gt;create!&lt;/code&gt;. Sometimes you intentionally want an invalid record, or you want a new record with just some valid attributes initialized. It&#8217;s also slower than fixtures, but I don&#8217;t care too much about that right now.&lt;/p&gt;


	&lt;p&gt;But there&#8217;s a bigger issue. Like a good little BDDer, I have also been stubbing and mocking all my &#8220;unit tests&#8221; so there is no interaction with the database or code outside of what is being tested. To make the factory method work, I now need to define 3 different methods depending on what I&#8217;m testing: one using &lt;code&gt;#create!&lt;/code&gt;, one using &lt;code&gt;#new&lt;/code&gt;, and another one using stubbing.&lt;/p&gt;


	&lt;h3&gt;What do we do about it?&lt;/h3&gt;


	&lt;p&gt;I think the FixtureReplacement plugin is on the right track. It handles the new vs. create problem very nicely.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;module FixtureReplacement
  attributes_for :person do |u|
    u.name     = String.random
    u.login    = String.random
    u.password = String.random
    u.account  = default_account
  end
end

@person = new_person(:login =&amp;gt; 'brandon')
@person = create_person&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Before I heard about the FixtureReplacement plugin, I actually concocted my own little solution that handles all three scenarios. I&#8217;m not crazy about the syntax, but it works for my needs.&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Generate.add Person, :name =&amp;gt; 'Brandon', :login =&amp;gt; 'brandon' do |generator, u|
  u.account ||= generator.account
end

@person = Generate.person(:login =&amp;gt; 'brandon')
@person = Generate.new_person
@person = Generate.stub_person&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Anywho, for the fixture-less approach to work, I think stubs need to be supported.&lt;/p&gt;


	&lt;h3&gt;Is all this really worth it?&lt;/h3&gt;


	&lt;p&gt;That&#8217;s the question I find myself asking. What&#8217;s wrong with fixtures anyway?&lt;/p&gt;


	&lt;p&gt;One thing I really like about fixtures, when done right, is that they help tell the story of your application. You get to know the fixture data as you work on the app.&lt;/p&gt;


	&lt;p&gt;The factory method also seems to go against convention-over-configuration. Instead of having a default &#8220;configuration&#8221; when your tests run, you have to configure each test. Call me lazy, but that just seems like too much work.&lt;/p&gt;


	&lt;p&gt;I find that testing takes a lot more effort now than it did with fixtures. And as a result, I&#8217;m more hesitant to test everything. So while the factory method and stubbing is theoretically supposed to help you test better, I feel like they&#8217;ve had the opposite effect.&lt;/p&gt;


	&lt;p&gt;What do you think? Have you had success using the factory pattern for tests?&lt;/p&gt;</content>  </entry>
</feed>
