<?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 Comments</title>
  <id>tag:opensoul.org,2009:/2008/8/21/life-without-fixtures/comments</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://opensoul.org/2008/8/21/life-without-fixtures/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
  <updated>2008-12-12T14:27:58Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>But well</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:9886</id>
    <published>2008-12-12T14:27:58Z</published>
    <updated>2008-12-12T14:27:58Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by But well</title>
<content type="html">&lt;p&gt;But fixtures are loaded in a database&#8230; I don&#8217;t get your point&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Derek</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:9880</id>
    <published>2008-12-11T21:43:39Z</published>
    <updated>2008-12-11T21:43:39Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by Derek</title>
<content type="html">&lt;p&gt;I find that there are cases where the correctness of your code relies on correct functioning of your finders. Since finders translate to &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt;, and you can&#8217;t test &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; without a database backend, I find that a fixture-less approach leads to some ugly hacking and empty tests, especially when shooting for full coverage.&lt;/p&gt;


	&lt;p&gt;As an example, I have the following code in a work application:&lt;/p&gt;


&lt;pre&gt;
def self.for(link)
  Score.find(:all,:conditions =&amp;gt; {:user_id =&amp;gt; link.user.id}, :order =&amp;gt; 'ABS(score) desc')
end
&lt;/pre&gt;

	&lt;p&gt;Without fixtures the only thing I can do there is stub out &#8220;find&#8221; on Score to return an array of mock Score objects, then check that the value I get back is the value I told it to return. Sure, the line is marked as covered, but it didn&#8217;t actually test a damn thing. Aside from manually testing the calling code through the web interface, I just have to take it on faith that the finder is correctly written.&lt;/p&gt;


	&lt;p&gt;With a small set of fixtures for the Score model, on the other hand, I can easily create a mock for &#8220;link&#8221;, pass it in, and check that the fixtures I get back really are the ones I expect, in the order I expect. In this way, I ensure that the parameters I send to &#8220;find&#8221; actually work. There is literally no way to do this without a database backend for your tests.&lt;/p&gt;


	&lt;p&gt;Actually, there is one way, and that&#8217;s to rewrite the code like this:&lt;/p&gt;


&lt;pre&gt;
Score.find(:all).select{|sc| sc.user_id == link.user.id}.sort{|a,b| b.score.abs &amp;lt;=&amp;gt; a.score.abs}
&lt;/pre&gt;

	&lt;p&gt;And in so doing, I&#8217;ve offloaded trivial database work into Ruby code. But hey, at least now I can test my code without fixtures!!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Well</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:9411</id>
    <published>2008-11-05T09:50:58Z</published>
    <updated>2008-11-05T09:50:58Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by Well</title>
<content type="html">&lt;p&gt;I use fixtures for two things:
1) for automated tests of course
2) and to populate my development database&lt;/p&gt;


	&lt;p&gt;Maybe I&#8217;m wrong for 2) but it helps me have an application with correct and human readable values. I can also recreate a state of the apps easily.&lt;/p&gt;


	&lt;p&gt;Can you do the same without fixtures?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>nofxx</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:8530</id>
    <published>2008-10-04T11:21:10Z</published>
    <updated>2008-10-04T11:21:10Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by nofxx</title>
<content type="html">&lt;p&gt;Object Daddy += 1&lt;/p&gt;


	&lt;p&gt;Looks simpler, and more elegant. Object.generate!&lt;/p&gt;


	&lt;p&gt;@Andrew: The test stuff stays on /spec/exemplars/mode_name_exemplar.rb. So it`s not loaded at runtime.
And the methods you end up doing are class methods like: next_valid_name , next_something&#8230;.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Andrew Vit</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:8028</id>
    <published>2008-09-09T02:39:39Z</published>
    <updated>2008-09-09T02:39:39Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by Andrew Vit</title>
<content type="html">&lt;p&gt;One more vote for factory_girl. (Though to be honest I haven&#8217;t given the others a fair shake). I don&#8217;t really see your point of convention over configuration with factory girl&#8230; Aren&#8217;t fixture files configuration too? I have one factory.rb file which defines all of my model defaults, with some handy things like sequences for unique emails or usernames, etc&#8230; I configure it once, then I can use them anywhere in my tests, relationships and all, and overriding the default objects in individual tests as needed. I haven&#8217;t tried working stubs into the mix, but I think where I want to stub, I want to do so explicitly&#8230; though a helper method to do so would be nice.&lt;/p&gt;


	&lt;p&gt;@David: I&#8217;m not sure I like the object_daddy idea of a generate method mixed into my models. It seems like something that might interfere with a method name&#8230;&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Scott Taylor</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:7982</id>
    <published>2008-09-06T03:38:07Z</published>
    <updated>2008-09-06T03:38:07Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by Scott Taylor</title>
<content type="html">&lt;p&gt;&#8220;The factory method also seems to go against convention-over-configuration. Instead of having a default “configuration” when your tests run, you have to configure each test. Call me lazy, but that just seems like too much work.&#8221;&lt;/p&gt;


	&lt;p&gt;Actually, one of the reasons I included String.random in FixtureReplacement was for &#8220;convention over configuration.&#8221; Each test case should express a unique situation &#8211; everything else which is irrelevant to your test case should go into before(:each) or elsewhere (FR puts all of it in one  file, example_data.rb since the data is &lt;strong&gt;so&lt;/strong&gt; generic).&lt;/p&gt;


	&lt;p&gt;David &#8211; I&#8217;d be interested if you used all three and if so, why you prefer ObjectDaddy over the rest.  Honestly, I find that putting default values right in the model to be &lt;strong&gt;really&lt;/strong&gt; gross.  Thoughts?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>David Chelimsky</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:7361</id>
    <published>2008-08-24T17:23:58Z</published>
    <updated>2008-08-24T17:23:58Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by David Chelimsky</title>
<content type="html">&lt;p&gt;I&#8217;ve been using &lt;a href=&quot;http://github.com/flogic/object_daddy&quot;&gt;object_daddy&lt;/a&gt; with some success. It adds generate and generate! methods to your model classes so you can do this:&lt;/p&gt;


&lt;pre&gt;
animal = Animal.generate!
&lt;/pre&gt;

	&lt;p&gt;It gives you a centralized location to define valid attributes, which can, of course, be overridden by passing a Hash to the generate! method &#8211; which you can use to keep relevant data in sight in your code examples:&lt;/p&gt;


&lt;pre&gt;
user = User.generate(:email =&amp;gt; 'poorly formatted email address'
user.should have(1).error_on(:email)
&lt;/pre&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Paul Barry</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:7226</id>
    <published>2008-08-21T21:19:55Z</published>
    <updated>2008-08-21T21:19:55Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by Paul Barry</title>
<content type="html">&lt;p&gt;I&#8217;ve been using fixture replacement for a little while now and I think it is great.  It keeps the data that is relevent to the test  right there with the test, making it&#8217;s intentions clear.  Factory Girl has been getting a lot of hype, but after looking at it briefly, fixture replacements seems cleaner, clearer to me.  Has anyone tried both fixture replacements and factory girl and found factory girl to be better?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Raj</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:7221</id>
    <published>2008-08-21T13:49:25Z</published>
    <updated>2008-08-21T13:49:25Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by Raj</title>
<content type="html">&lt;p&gt;Factory girl is really really nice. Give it a try.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:7220</id>
    <published>2008-08-21T13:36:38Z</published>
    <updated>2008-08-21T13:36:38Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by Brandon</title>
<content type="html">&lt;p&gt;You probably have heard of &lt;a href=&quot;http://www.thoughtbot.com/projects/shoulda&quot;&gt;Shoulda&lt;/a&gt; and &lt;a href=&quot;http://www.thoughtbot.com/projects/factory_girl&quot;&gt;factory_girl&lt;/a&gt; from ThoughtBot.  I would recommend giving them a look, as I have found them easy to work with for the factory pattern.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Zach Dennis</name>
    </author>
    <id>tag:opensoul.org,2008-08-21:7207:7209</id>
    <published>2008-08-21T03:35:09Z</published>
    <updated>2008-08-21T03:35:09Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2008/8/21/life-without-fixtures" rel="alternate" type="text/html"/>
    <title>Comment on 'Life without fixtures' by Zach Dennis</title>
<content type="html">&lt;p&gt;This is probably going to be ginormously long. I apologize in advance. =)&lt;/p&gt;


	&lt;p&gt;It&#8217;s been so long since I&#8217;ve used fixtures! I can&#8217;t say that I&#8217;ve missed them or any of their features. I&#8217;m sure that when they are used properly along side stubs and mocks they are much less of a pain, but I apparently wasn&#8217;t smart enough (perhaps now I am) to avoid fixture nightmares.&lt;/p&gt;


	&lt;p&gt;For the past several months I&#8217;ve been using the factory pattern with success. There have been some things I&#8217;ve found along the way similar to what you&#8217;ve mentioned: needing to have methods which build &#8220;new&#8221; objects and having methods which create &#8220;valid&#8221; objects. Recently I&#8217;ve even split out the valid attributes into their own module, ValidAttributes. My factory uses them to construct objects. ie:&lt;/p&gt;


&lt;pre&gt;
module ValidAttributes
  def valid_day_attributes
    { :date =&amp;gt; Date.today }
  end
end

module Generate
  def self.new_day(attrs={})
    Day.new valid_day_attributes.merge(attrs)
  end

  def self.day(attrs={})
    day = new_day(attrs)
    day.save!
    day
  end
end
&lt;/pre&gt;

	&lt;p&gt;The thing I like most about using a factory is that it allows the examples to be very explicit what is needed (or not needed) for a particular behavior. I don&#8217;t like having to scan through multiple files to understand an example. I just want to read the example to see all the pertinent information.&lt;/p&gt;


	&lt;p&gt;Regardless, I need to know what I want to get out of my factory or fixtures or anything else that tries to fill the gap. I need to be able to build/create valid objects, build/create invalid objects, construct intertwined objects (relationships) and also create objects which are a fair real-world representation of a sample set of data. I also want the important pieces to be highly visible in examples, while the non-important pieces are non-visible (or at least not competing with the important stuff for visibility).&lt;/p&gt;


	&lt;p&gt;With everything though I realize mileage can vary. My experience with factories have been good so far. My experience with fixtures were not so good.&lt;/p&gt;</content>  </entry>
</feed>
