<?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>Sat, 13 Mar 2010 21:45:08 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<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>
<pre>it "should have an active named_scope on status" do
    Project.active.proxy_options.should == {:conditions =&gt; {:status =&gt; "active"}, :order =&gt; 'created_at DESC'}
 end</pre>
<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>
<pre>
class Foo < ActiveRecord::Base
  named_scope :active, :conditions => { :active => true }
end

test "named_scope :active symbol" do # won't fail
  expected = { :conditions => { :active => true } }
  assert_equal expected, Foo.active.proxy_options
end

test "named_scope :active string" do # will fail
  expected = { :conditions => { "active" => true } }
  assert_equal expected, Foo.active.proxy_options
end

test "named_scope :active replacement" do # will fail
  expected = { :conditions => ['active = ?', true] }
  assert_equal expected, Foo.active.proxy_options
end

test "named_scope :active replacement with table" do # will fail
  expected = { :conditions => ['foos.active = ?', true] }
  assert_equal expected, Foo.active.proxy_options
end
</pre>
<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>
<pre>
class Foo < ActiveRecord::Base
    named_scope :latest, lambda { |*args| { :limit => (args.shift || nil), :order => "#{self.table_name}.created_at DESC" }}
end
</pre>
<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>
