<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Testing in Rails: Part 7 &#8211; ActiveRecord Relationships</title>
	<atom:link href="http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/</link>
	<description>Code for Coders</description>
	<lastBuildDate>Sat, 31 Dec 2011 17:28:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
	<item>
		<title>By: Kevin Skoglund</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-2419</link>
		<dc:creator>Kevin Skoglund</dc:creator>
		<pubDate>Mon, 17 May 2010 16:10:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-2419</guid>
		<description>Steve: Did you give sunnyside an explicit ID?  Maybe the YAML formatting is off?  I believe the current behavior is to use the ID if provided, otherwise to create a hash of the label to use.

Here are more details on how it works:
http://ryandaigle.com/articles/2007/10/26/what-s-new-in-edge-rails-fixtures-just-got-a-whole-lot-easier

Since I wrote this, it has become more common to create fixtures *without* providing an explicit ID and to use the hashed value of the name as the ID instead.  But both should still work.</description>
		<content:encoded><![CDATA[<p>Steve: Did you give sunnyside an explicit ID?  Maybe the YAML formatting is off?  I believe the current behavior is to use the ID if provided, otherwise to create a hash of the label to use.</p>
<p>Here are more details on how it works:<br />
<a href="http://ryandaigle.com/articles/2007/10/26/what-s-new-in-edge-rails-fixtures-just-got-a-whole-lot-easier" rel="nofollow">http://ryandaigle.com/articles/2007/10/26/what-s-new-in-edge-rails-fixtures-just-got-a-whole-lot-easier</a></p>
<p>Since I wrote this, it has become more common to create fixtures *without* providing an explicit ID and to use the hashed value of the name as the ID instead.  But both should still work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Dame</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-2413</link>
		<dc:creator>Steve Dame</dc:creator>
		<pubDate>Fri, 14 May 2010 21:01:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-2413</guid>
		<description>This was by far the most useful column I&#039;ve seen on testing.  Thanks very much!  The one thing that lingers as confusing for me is that my actual ID generated by rails doesn&#039;t seem to match up with a manually hashed value of the symbol.

For example, in order to test assert_equal(1, @merlot.winery_id), this fails because my rails version (2.3.5) hashes the name of the YAML row label &quot;sunnyside:&quot; in the wineries.yml fixture and loads that into the database as 350710738.  So in order to test this I have to do either the following:

assert_equal(350710738, @merlot.winery_id)  OR
assert_equal(@merlot.winery.id, @merlot.winery_id)

If, in IRB, I manually check the value of sunnyside, it is

irb&gt; &quot;sunnyside&quot;.hash.abs 
=&gt; 528506201

My fixture is definitely using the label

sunnyside:
    ...

Do you know if for some reason this version of rails is appending characters to symbols in order to determine the ID values?  Seems there is an extra string added or something?</description>
		<content:encoded><![CDATA[<p>This was by far the most useful column I&#8217;ve seen on testing.  Thanks very much!  The one thing that lingers as confusing for me is that my actual ID generated by rails doesn&#8217;t seem to match up with a manually hashed value of the symbol.</p>
<p>For example, in order to test assert_equal(1, @merlot.winery_id), this fails because my rails version (2.3.5) hashes the name of the YAML row label &#8220;sunnyside:&#8221; in the wineries.yml fixture and loads that into the database as 350710738.  So in order to test this I have to do either the following:</p>
<p>assert_equal(350710738, @merlot.winery_id)  OR<br />
assert_equal(@merlot.winery.id, @merlot.winery_id)</p>
<p>If, in IRB, I manually check the value of sunnyside, it is</p>
<p>irb&gt; &#8220;sunnyside&#8221;.hash.abs<br />
=&gt; 528506201</p>
<p>My fixture is definitely using the label</p>
<p>sunnyside:<br />
    &#8230;</p>
<p>Do you know if for some reason this version of rails is appending characters to symbols in order to determine the ID values?  Seems there is an extra string added or something?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Skoglund</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-2411</link>
		<dc:creator>Kevin Skoglund</dc:creator>
		<pubDate>Fri, 07 May 2010 16:18:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-2411</guid>
		<description>Steve: All the fixtures are shown, either on this post or in the previous one.</description>
		<content:encoded><![CDATA[<p>Steve: All the fixtures are shown, either on this post or in the previous one.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-2410</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Thu, 06 May 2010 20:02:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-2410</guid>
		<description>Very helpful, but could you publish (and/or e-mail me) the contents of your fixtures?  I&#039;m still sorting through the confusing of trying out relationship associations from the console vs. in the test scripts.</description>
		<content:encoded><![CDATA[<p>Very helpful, but could you publish (and/or e-mail me) the contents of your fixtures?  I&#8217;m still sorting through the confusing of trying out relationship associations from the console vs. in the test scripts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brandon</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-2399</link>
		<dc:creator>Brandon</dc:creator>
		<pubDate>Fri, 09 Apr 2010 18:48:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-2399</guid>
		<description>Your post has been extremely useful at helping me understand the nature of AR associations and how to test them.  Thank you!.  I recently discovered that the shoulda gem contains macros for testing these associations as well.  I highly recommend any readers first use this post to understand the nature of testing the associations and then afterwards check out implementing shoulda so you can do things like:

should_have_many :tags, :through =&gt; :taggings

http://wiki.github.com/thoughtbot/shoulda/usage</description>
		<content:encoded><![CDATA[<p>Your post has been extremely useful at helping me understand the nature of AR associations and how to test them.  Thank you!.  I recently discovered that the shoulda gem contains macros for testing these associations as well.  I highly recommend any readers first use this post to understand the nature of testing the associations and then afterwards check out implementing shoulda so you can do things like:</p>
<p>should_have_many :tags, :through =&gt; :taggings</p>
<p><a href="http://wiki.github.com/thoughtbot/shoulda/usage" rel="nofollow">http://wiki.github.com/thoughtbot/shoulda/usage</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Taylor</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-1350</link>
		<dc:creator>Taylor</dc:creator>
		<pubDate>Thu, 08 Jan 2009 17:07:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-1350</guid>
		<description>This is an excellent post!  I&#039;m glad I finally found it.  I have only one comment - I would suggest NOT having one big &quot;test_relationships&quot; method, but rather breaking up the tests into, for example, &quot;test_belongs_to_winery&quot; and &quot;test_has_and_belongs_to_many_varietals&quot;.

This is partly personal style, but there is a good reason behind it too.  For one thing, if you have one big method, and the belongs_to test breaks, it precludes the later tests from being run.  So one failure can mask another.  For another, using smaller methods lets you name them more descriptively.

Great post though - thanks!</description>
		<content:encoded><![CDATA[<p>This is an excellent post!  I&#8217;m glad I finally found it.  I have only one comment &#8211; I would suggest NOT having one big &#8220;test_relationships&#8221; method, but rather breaking up the tests into, for example, &#8220;test_belongs_to_winery&#8221; and &#8220;test_has_and_belongs_to_many_varietals&#8221;.</p>
<p>This is partly personal style, but there is a good reason behind it too.  For one thing, if you have one big method, and the belongs_to test breaks, it precludes the later tests from being run.  So one failure can mask another.  For another, using smaller methods lets you name them more descriptively.</p>
<p>Great post though &#8211; thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marty McGee</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-1314</link>
		<dc:creator>Marty McGee</dc:creator>
		<pubDate>Thu, 27 Nov 2008 15:50:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-1314</guid>
		<description>This is by far the most helpful post about Rails 2+ model relationship testing I have found.  And just to keep the ball rolling, I wanted to point out that with Foxy Fixtures in Rails 2+, we don&#039;t even need to setup the join table fixtures (in this case, &quot;varietals_wines&quot;).  Saves some time and effort and keeps tests running accurately in the future.  For instance, with the awesome HABTM relationship test Kevin has shared with us, we can simply omit the varietals_wines.yml fixture and have our wine unit test look this this...

class WineTest &lt; ActiveSupport::TestCase
  # omitted unneeded :varietals_wines fixture
  fixtures :wines, :wineries, :varietals

  def setup
    ...
    @bordeaux = wines(:bordeaux)
  end
 
  def test_relationships
    ...
    # has_and_belongs_to_many :varietals
    assert_equal(3, @bordeaux.varietals.count)
    @bordeaux.varietals.each do &#124;var&#124;
      assert(var.wines.include?(@bordeaux))
    end
    (Varietal.find(:all) - @bordeaux.varietals).each do &#124;var&#124;
      assert(!var.wines.include?(@bordeaux))
    end
    assert_equal(&quot;Cabernet Franc&quot;, @bordeaux.varietals.sort_by {&#124;v&#124; v.name}.first.name)
  end
end</description>
		<content:encoded><![CDATA[<p>This is by far the most helpful post about Rails 2+ model relationship testing I have found.  And just to keep the ball rolling, I wanted to point out that with Foxy Fixtures in Rails 2+, we don&#8217;t even need to setup the join table fixtures (in this case, &#8220;varietals_wines&#8221;).  Saves some time and effort and keeps tests running accurately in the future.  For instance, with the awesome HABTM relationship test Kevin has shared with us, we can simply omit the varietals_wines.yml fixture and have our wine unit test look this this&#8230;</p>
<p>class WineTest &lt; ActiveSupport::TestCase<br />
  # omitted unneeded :varietals_wines fixture<br />
  fixtures :wines, :wineries, :varietals</p>
<p>  def setup<br />
    &#8230;<br />
    @bordeaux = wines(:bordeaux)<br />
  end</p>
<p>  def test_relationships<br />
    &#8230;<br />
    # has_and_belongs_to_many :varietals<br />
    assert_equal(3, @bordeaux.varietals.count)<br />
    @bordeaux.varietals.each do |var|<br />
      assert(var.wines.include?(@bordeaux))<br />
    end<br />
    (Varietal.find(:all) &#8211; @bordeaux.varietals).each do |var|<br />
      assert(!var.wines.include?(@bordeaux))<br />
    end<br />
    assert_equal(&#8220;Cabernet Franc&#8221;, @bordeaux.varietals.sort_by {|v| v.name}.first.name)<br />
  end<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Skoglund</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-1231</link>
		<dc:creator>Kevin Skoglund</dc:creator>
		<pubDate>Mon, 04 Aug 2008 13:48:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-1231</guid>
		<description>@Denis: Yes, that would work too.  As long as you understand the difference.

In my example, the idea is that I&#039;m testing two things to make sure they are the EXACT value I expect.  In your example, the idea is that you don&#039;t care what value they have, just that they are equal.  If @merlot.winery_id and @merlot.winery.id both have a value of 4 then my tests will fail and yours will pass.

The way you decide to write your tests will determine if that distinction makes a difference or not.</description>
		<content:encoded><![CDATA[<p>@Denis: Yes, that would work too.  As long as you understand the difference.</p>
<p>In my example, the idea is that I&#8217;m testing two things to make sure they are the EXACT value I expect.  In your example, the idea is that you don&#8217;t care what value they have, just that they are equal.  If @merlot.winery_id and @merlot.winery.id both have a value of 4 then my tests will fail and yours will pass.</p>
<p>The way you decide to write your tests will determine if that distinction makes a difference or not.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Denis</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-1230</link>
		<dc:creator>Denis</dc:creator>
		<pubDate>Mon, 04 Aug 2008 03:56:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-1230</guid>
		<description>In the first relationship test, you wrote:
  def test_relationships
    # belongs_to :winery
    assert_equal(1, @merlot.winery_id)
    assert_equal(1, @merlot.winery.id)
[...]

Could we just write this?
  def test_relationships
    # belongs_to :winery
    assert_equal(@merlot.winery.id, @merlot.winery_id)
[...]


Thanks
Denis</description>
		<content:encoded><![CDATA[<p>In the first relationship test, you wrote:<br />
  def test_relationships<br />
    # belongs_to :winery<br />
    assert_equal(1, @merlot.winery_id)<br />
    assert_equal(1, @merlot.winery.id)<br />
[...]</p>
<p>Could we just write this?<br />
  def test_relationships<br />
    # belongs_to :winery<br />
    assert_equal(@merlot.winery.id, @merlot.winery_id)<br />
[...]</p>
<p>Thanks<br />
Denis</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Improbabilidades</title>
		<link>http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/comment-page-1/#comment-1064</link>
		<dc:creator>Improbabilidades</dc:creator>
		<pubDate>Tue, 18 Mar 2008 14:12:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/08/testing-in-rails-part-7-activerecord-relationships/#comment-1064</guid>
		<description>&lt;strong&gt;Testing relationships in&#160;Rails...&lt;/strong&gt;

Ler este artigo em português
I&#039;ve been looking for an elegant way to test relationships using Rais, and think it&#039;s interesting how so few people really care about it. Most simply say that it&#039;s up to the framework to test it&#039;s o...</description>
		<content:encoded><![CDATA[<p><strong>Testing relationships in&nbsp;Rails&#8230;</strong></p>
<p>Ler este artigo em português<br />
I&#8217;ve been looking for an elegant way to test relationships using Rais, and think it&#8217;s interesting how so few people really care about it. Most simply say that it&#8217;s up to the framework to test it&#8217;s o&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

