<?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 Rails: How to test Rails ActiveRecord Named Scopes</title>
	<atom:link href="http://www.simonecarletti.com/blog/2009/06/how-to-test-rails-activerecord-named-scopes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.simonecarletti.com/blog/2009/06/how-to-test-rails-activerecord-named-scopes/</link>
	<description>Simone Carletti&#039;s personal ramblings on programming, syndication, search engines &#38; marketing.</description>
	<lastBuildDate>Thu, 09 Feb 2012 14:24:07 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: james2m</title>
		<link>http://www.simonecarletti.com/blog/2009/06/how-to-test-rails-activerecord-named-scopes/#comment-13673</link>
		<dc:creator>james2m</dc:creator>
		<pubDate>Sat, 31 Jul 2010 12:37:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=392#comment-13673</guid>
		<description>
&quot;    setup do
      @clients = 3.times.map { Invitation.make }
      @advisors = 2.times.map { Invitation.make(:advisor) }
      @creditors = 1.times.map { Invitation.make(:creditor) }
    end

    should &quot;return only client invitations&quot; do
      assert_equal @clients, Invitation.clients
    end
    
    should &quot;return only advisor invitations&quot; do
      assert_equal @advisors, Invitation.advisors
    end
    
    should &quot;return only creditor invitations&quot; do
      assert_equal @creditor, Invitation.creditor
    end&quot;
</description>
		<content:encoded><![CDATA[<p>&#8221;    setup do<br />
      @clients = 3.times.map { Invitation.make }<br />
      @advisors = 2.times.map { Invitation.make(:advisor) }<br />
      @creditors = 1.times.map { Invitation.make(:creditor) }<br />
    end</p>
<p>    should &#8220;return only client invitations&#8221; do<br />
      assert_equal @clients, Invitation.clients<br />
    end</p>
<p>    should &#8220;return only advisor invitations&#8221; do<br />
      assert_equal @advisors, Invitation.advisors<br />
    end</p>
<p>    should &#8220;return only creditor invitations&#8221; do<br />
      assert_equal @creditor, Invitation.creditor<br />
    end&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phillip</title>
		<link>http://www.simonecarletti.com/blog/2009/06/how-to-test-rails-activerecord-named-scopes/#comment-10507</link>
		<dc:creator>Phillip</dc:creator>
		<pubDate>Wed, 21 Apr 2010 17:06:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=392#comment-10507</guid>
		<description>I know it&#039;s been many months since this post was made, but since comments are still open, I thought I&#039;d toss in my 1/2 cent.

I agree with @James and @Peter. I&#039;ve recently been catching myself paying too much attention to implementation and have had to back off a refocus on the intended behavior. There have been a few times in the past that I&#039;ve implemented something one way and then come back and reworked it when business needs changed. Having a behavior-oriented test in place won&#039;t be as brittle as an implementation-oriented and will allow you to properly refactor your code using the test as an indicator that the refactor is successful.

I agree with the respond_to? part though. I have a section in my spec for methods, delegates, and whatever else I may call in code to make sure I have defined them. Then I have another describe for the behavior. It works well for me.

Peace.</description>
		<content:encoded><![CDATA[<p>I know it&#8217;s been many months since this post was made, but since comments are still open, I thought I&#8217;d toss in my 1/2 cent.</p>
<p>I agree with @James and @Peter. I&#8217;ve recently been catching myself paying too much attention to implementation and have had to back off a refocus on the intended behavior. There have been a few times in the past that I&#8217;ve implemented something one way and then come back and reworked it when business needs changed. Having a behavior-oriented test in place won&#8217;t be as brittle as an implementation-oriented and will allow you to properly refactor your code using the test as an indicator that the refactor is successful.</p>
<p>I agree with the respond_to? part though. I have a section in my spec for methods, delegates, and whatever else I may call in code to make sure I have defined them. Then I have another describe for the behavior. It works well for me.</p>
<p>Peace.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.simonecarletti.com/blog/2009/06/how-to-test-rails-activerecord-named-scopes/#comment-4169</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Fri, 21 Aug 2009 16:07:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=392#comment-4169</guid>
		<description>I agree with @James - you&#039;re just testing the implementation of your proxy, which doesn&#039;t make sense. Unit testing means testing the behaviour of the class or object as it appears to the outside world.

I did learn something neat tho - proxy_options is useful to know about. Thanks!</description>
		<content:encoded><![CDATA[<p>I agree with @James &#8211; you&#8217;re just testing the implementation of your proxy, which doesn&#8217;t make sense. Unit testing means testing the behaviour of the class or object as it appears to the outside world.</p>
<p>I did learn something neat tho &#8211; proxy_options is useful to know about. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: olivernn</title>
		<link>http://www.simonecarletti.com/blog/2009/06/how-to-test-rails-activerecord-named-scopes/#comment-3987</link>
		<dc:creator>olivernn</dc:creator>
		<pubDate>Tue, 11 Aug 2009 21:18:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=392#comment-3987</guid>
		<description>Sorry for posting on an old post, just thought I&#039;d throw in how I test named_scopes.  Instead of testing the actual methods for named_scope, which I would assume are fairly well tested, I want to make sure that a particular model has a named scope with the right conditions.

&lt;pre&gt;it &quot;should have an active named_scope on status&quot; do
    Project.active.proxy_options.should == {:conditions =&gt; {:status =&gt; &quot;active&quot;}, :order =&gt; &#039;created_at DESC&#039;}
 end&lt;/pre&gt;

I use rspec, but really I think the idea should be the same no matter what library for testing you use.</description>
		<content:encoded><![CDATA[<p>Sorry for posting on an old post, just thought I&#8217;d throw in how I test named_scopes.  Instead of testing the actual methods for named_scope, which I would assume are fairly well tested, I want to make sure that a particular model has a named scope with the right conditions.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">it &quot;should have an active named_scope on status&quot; do<br />
&nbsp; &nbsp; Project.active.proxy_options.should == {:conditions =&gt; {:status =&gt; &quot;active&quot;}, :order =&gt; 'created_at DESC'}<br />
&nbsp;end</div></td></tr></tbody></table></div>
<p>I use rspec, but really I think the idea should be the same no matter what library for testing you use.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simone Carletti</title>
		<link>http://www.simonecarletti.com/blog/2009/06/how-to-test-rails-activerecord-named-scopes/#comment-3136</link>
		<dc:creator>Simone Carletti</dc:creator>
		<pubDate>Thu, 18 Jun 2009 10:13:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=392#comment-3136</guid>
		<description>Hey James,
thanks for stopping by this post.

I agree with you in part and, what I probably forgot to say in my post, it that there are some situation where it actually makes sense to run a database query.

For instance, testing the raw proxy_options can cause a test to fail just because you are not coding the test in the same way you coded the scope.

In the following case, the test will fail even though the query would return the same recordset regardless you are using strings or symbols.

&lt;pre&gt;
class Foo &lt; ActiveRecord::Base
  named_scope :active, :conditions =&gt; { :active =&gt; true }
end

test &quot;named_scope :active symbol&quot; do # won&#039;t fail
  expected = { :conditions =&gt; { :active =&gt; true } }
  assert_equal expected, Foo.active.proxy_options
end

test &quot;named_scope :active string&quot; do # will fail
  expected = { :conditions =&gt; { &quot;active&quot; =&gt; true } }
  assert_equal expected, Foo.active.proxy_options
end

test &quot;named_scope :active replacement&quot; do # will fail
  expected = { :conditions =&gt; [&#039;active = ?&#039;, true] }
  assert_equal expected, Foo.active.proxy_options
end

test &quot;named_scope :active replacement with table&quot; do # will fail
  expected = { :conditions =&gt; [&#039;foos.active = ?&#039;, true] }
  assert_equal expected, Foo.active.proxy_options
end
&lt;/pre&gt;

This is probably the main reason why proxy_option might not be a good candidate for tests in some cases.

An other good one is that testing proxy_options doesn&#039;t ensure you are passing invalid SQL statements to the database. This happened to me in some circumstances where I had to deal with SQLite in development mode and PostgreSQL in production or staging environment.

But the following one, IMHO, is probably a good candidate. Generally, I would prefer testing the specific proxy options when I have fair complex named_scopes (assuming you specifically want to use a named scope and not a classic method).

&lt;pre&gt;
class Foo &lt; ActiveRecord::Base
    named_scope :latest, lambda { &#124;*args&#124; { :limit =&gt; (args.shift &#124;&#124; nil), :order =&gt; &quot;#{self.table_name}.created_at DESC&quot; }}
end
&lt;/pre&gt;

In this case, you might want to setup a test to stress the different behaviors and eventually an other one to test the effective result of the query.

Many thanks for your comment. It gave me the opportunity to add some more details to the article. :)</description>
		<content:encoded><![CDATA[<p>Hey James,<br />
thanks for stopping by this post.</p>
<p>I agree with you in part and, what I probably forgot to say in my post, it that there are some situation where it actually makes sense to run a database query.</p>
<p>For instance, testing the raw proxy_options can cause a test to fail just because you are not coding the test in the same way you coded the scope.</p>
<p>In the following case, the test will fail even though the query would return the same recordset regardless you are using strings or symbols.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">class Foo &lt; ActiveRecord::Base<br />
&nbsp; named_scope :active, :conditions =&gt; { :active =&gt; true }<br />
end<br />
<br />
test &quot;named_scope :active symbol&quot; do # won't fail<br />
&nbsp; expected = { :conditions =&gt; { :active =&gt; true } }<br />
&nbsp; assert_equal expected, Foo.active.proxy_options<br />
end<br />
<br />
test &quot;named_scope :active string&quot; do # will fail<br />
&nbsp; expected = { :conditions =&gt; { &quot;active&quot; =&gt; true } }<br />
&nbsp; assert_equal expected, Foo.active.proxy_options<br />
end<br />
<br />
test &quot;named_scope :active replacement&quot; do # will fail<br />
&nbsp; expected = { :conditions =&gt; ['active = ?', true] }<br />
&nbsp; assert_equal expected, Foo.active.proxy_options<br />
end<br />
<br />
test &quot;named_scope :active replacement with table&quot; do # will fail<br />
&nbsp; expected = { :conditions =&gt; ['foos.active = ?', true] }<br />
&nbsp; assert_equal expected, Foo.active.proxy_options<br />
end</div></td></tr></tbody></table></div>
<p>This is probably the main reason why proxy_option might not be a good candidate for tests in some cases.</p>
<p>An other good one is that testing proxy_options doesn&#8217;t ensure you are passing invalid SQL statements to the database. This happened to me in some circumstances where I had to deal with SQLite in development mode and PostgreSQL in production or staging environment.</p>
<p>But the following one, IMHO, is probably a good candidate. Generally, I would prefer testing the specific proxy options when I have fair complex named_scopes (assuming you specifically want to use a named scope and not a classic method).</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">class Foo &lt; ActiveRecord::Base<br />
&nbsp; &nbsp; named_scope :latest, lambda { |*args| { :limit =&gt; (args.shift || nil), :order =&gt; &quot;#{self.table_name}.created_at DESC&quot; }}<br />
end</div></td></tr></tbody></table></div>
<p>In this case, you might want to setup a test to stress the different behaviors and eventually an other one to test the effective result of the query.</p>
<p>Many thanks for your comment. It gave me the opportunity to add some more details to the article. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Adam</title>
		<link>http://www.simonecarletti.com/blog/2009/06/how-to-test-rails-activerecord-named-scopes/#comment-3135</link>
		<dc:creator>James Adam</dc:creator>
		<pubDate>Thu, 18 Jun 2009 09:48:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.simonecarletti.com/blog/?p=392#comment-3135</guid>
		<description>I&#039;m not sure I prefer this - you&#039;re only testing the implementation details of the named scope here, rather than the desired behaviour. 

At the very least you&#039;d need to ensure that somewhere there was an integration test which properly exercised the method (which happens to be a named scope right now, but may not be in the future) against a set of matching and non-matching records.

I understand the desire to keep tests speedy and decoupled, but I think you&#039;ll struggle to use your final suite of tests to refactor the implementation later on...</description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure I prefer this &#8211; you&#8217;re only testing the implementation details of the named scope here, rather than the desired behaviour. </p>
<p>At the very least you&#8217;d need to ensure that somewhere there was an integration test which properly exercised the method (which happens to be a named scope right now, but may not be in the future) against a set of matching and non-matching records.</p>
<p>I understand the desire to keep tests speedy and decoupled, but I think you&#8217;ll struggle to use your final suite of tests to refactor the implementation later on&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

