<?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 8 &#8211; Validations</title>
	<atom:link href="http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/</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: Mike Blyth</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-2461</link>
		<dc:creator>Mike Blyth</dc:creator>
		<pubDate>Mon, 27 Sep 2010 07:57:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-2461</guid>
		<description>One caveat when using cloned objects. Apparently, if the original object has already been validated, then the clone will pass the validation tests even if you change the attributes to make them invalid. To avoid this problem:

From an explanation on StackOverflow by Pawein at http://bit.ly/adP1n1: &quot;The solution is simple - just set @errors to nil after cloning and before validation. As it is quite private, the simple assignment doesn&#039;t work. But this works:

cloned.instance_eval do
  @errors = nil
end</description>
		<content:encoded><![CDATA[<p>One caveat when using cloned objects. Apparently, if the original object has already been validated, then the clone will pass the validation tests even if you change the attributes to make them invalid. To avoid this problem:</p>
<p>From an explanation on StackOverflow by Pawein at <a href="http://bit.ly/adP1n1" rel="nofollow">http://bit.ly/adP1n1</a>: &#8220;The solution is simple &#8211; just set @errors to nil after cloning and before validation. As it is quite private, the simple assignment doesn&#8217;t work. But this works:</p>
<p>cloned.instance_eval do<br />
  @errors = nil<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Blyth</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-2460</link>
		<dc:creator>Mike Blyth</dc:creator>
		<pubDate>Sun, 26 Sep 2010 20:33:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-2460</guid>
		<description>This is a great tutorial! One thing to note is that the assert_valid method is now gone, and we&#039;re supposed to use assert(thing.valid?) instead. Of course, since you&#039;ve given us the assert_invalid wrapper, we could do the same for assert_valid.

Anyone, what does Nicos Picos mean by having to use a clone for temp so that it doesn&#039;t lose its reference?</description>
		<content:encoded><![CDATA[<p>This is a great tutorial! One thing to note is that the assert_valid method is now gone, and we&#8217;re supposed to use assert(thing.valid?) instead. Of course, since you&#8217;ve given us the assert_invalid wrapper, we could do the same for assert_valid.</p>
<p>Anyone, what does Nicos Picos mean by having to use a clone for temp so that it doesn&#8217;t lose its reference?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sk</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-2159</link>
		<dc:creator>sk</dc:creator>
		<pubDate>Wed, 27 May 2009 11:29:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-2159</guid>
		<description>Hi
    For winery_test.rb I wrote
    	assert_required_length_less_than(@sunnyside,:name,255)
	assert_required_length_less_than(@sunnyside,:city,255)
	assert_required_length_less_than(@sunnyside,:state,255)
	assert_required_length_less_than(@sunnyside,:country,255)
 
     But when run the tests i get failure
 [/test/test_helper.rb:52:in `assert_required_length_less_than&#039;
 /test/unit/winery_test.rb:25:in `__bind_1243421822_280098&#039;]:
 Errors: name has already been taken (&quot;Sunnyside Vineyards&quot;).
  is not true.
     Why this happens?
sk</description>
		<content:encoded><![CDATA[<p>Hi<br />
    For winery_test.rb I wrote<br />
    	assert_required_length_less_than(@sunnyside,:name,255)<br />
	assert_required_length_less_than(@sunnyside,:city,255)<br />
	assert_required_length_less_than(@sunnyside,:state,255)<br />
	assert_required_length_less_than(@sunnyside,:country,255)</p>
<p>     But when run the tests i get failure<br />
 [/test/test_helper.rb:52:in `assert_required_length_less_than'<br />
 /test/unit/winery_test.rb:25:in `__bind_1243421822_280098']:<br />
 Errors: name has already been taken (&#8220;Sunnyside Vineyards&#8221;).<br />
  is not true.<br />
     Why this happens?<br />
sk</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ujihisa</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-1379</link>
		<dc:creator>ujihisa</dc:creator>
		<pubDate>Tue, 17 Mar 2009 09:42:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-1379</guid>
		<description>alias isn&#039;t a class method but a instruction. That comma is not needed :-)</description>
		<content:encoded><![CDATA[<p>alias isn&#8217;t a class method but a instruction. That comma is not needed <img src='http://www.nullislove.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David T.</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-1292</link>
		<dc:creator>David T.</dc:creator>
		<pubDate>Tue, 21 Oct 2008 11:43:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-1292</guid>
		<description>I guess

alias :assert_invalid :assert_not_valid

should have been

alias :assert_invalid, :assert_not_valid

with a comma.</description>
		<content:encoded><![CDATA[<p>I guess</p>
<p>alias :assert_invalid :assert_not_valid</p>
<p>should have been</p>
<p>alias :assert_invalid, :assert_not_valid</p>
<p>with a comma.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicos Picos</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-1213</link>
		<dc:creator>Nicos Picos</dc:creator>
		<pubDate>Wed, 16 Jul 2008 15:50:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-1213</guid>
		<description>Man.. really wish I had used helpers for the relationship stuff. Thanks for this tutorial, there is little good stuff on testing out there - and I found this very thorough and well written!</description>
		<content:encoded><![CDATA[<p>Man.. really wish I had used helpers for the relationship stuff. Thanks for this tutorial, there is little good stuff on testing out there &#8211; and I found this very thorough and well written!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicos Picos</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-1212</link>
		<dc:creator>Nicos Picos</dc:creator>
		<pubDate>Wed, 16 Jul 2008 15:49:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-1212</guid>
		<description>I recommend for the helper that tests presence_of to use clone objects, because otherwise an error will be thrown in the reference for temp. In your example it works because you are just checking for the presence of literals. I found I was usually testing for the presence of other objects. Here is the edited code I used:

  # helper method used for validates_presence of
  def assert_presence_required(object, field)
    
    # Test that the initial object is valid
    assert_valid(object)
   
    # Test that it becomes invalid by removing the field
    temp = object.send(field)
    
    # CHANGE = use a clone because otherwise reference to the temp object is lost
    tempClone = temp.clone 
    object.send(&quot;#{field}=&quot;, nil)
    assert_invalid(object)
    assert(object.errors.invalid?(field), &quot;Expected an error on validation&quot;)

    # CHANGE = set it to the clone now to make object valid again.
    object.send(&quot;#{field}=&quot;, tempClone)
    assert_valid(object)</description>
		<content:encoded><![CDATA[<p>I recommend for the helper that tests presence_of to use clone objects, because otherwise an error will be thrown in the reference for temp. In your example it works because you are just checking for the presence of literals. I found I was usually testing for the presence of other objects. Here is the edited code I used:</p>
<p>  # helper method used for validates_presence of<br />
  def assert_presence_required(object, field)</p>
<p>    # Test that the initial object is valid<br />
    assert_valid(object)</p>
<p>    # Test that it becomes invalid by removing the field<br />
    temp = object.send(field)</p>
<p>    # CHANGE = use a clone because otherwise reference to the temp object is lost<br />
    tempClone = temp.clone<br />
    object.send(&#8220;#{field}=&#8221;, nil)<br />
    assert_invalid(object)<br />
    assert(object.errors.invalid?(field), &#8220;Expected an error on validation&#8221;)</p>
<p>    # CHANGE = set it to the clone now to make object valid again.<br />
    object.send(&#8220;#{field}=&#8221;, tempClone)<br />
    assert_valid(object)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Skoglund</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-1211</link>
		<dc:creator>Kevin Skoglund</dc:creator>
		<pubDate>Wed, 16 Jul 2008 15:47:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-1211</guid>
		<description>There is no reason not to use helpers anytime you are repeating yourself.  They make testing a lot easier.  I didn&#039;t use them through this tutorial because I wanted to make sure that the core concepts were clear.</description>
		<content:encoded><![CDATA[<p>There is no reason not to use helpers anytime you are repeating yourself.  They make testing a lot easier.  I didn&#8217;t use them through this tutorial because I wanted to make sure that the core concepts were clear.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nicos Picos</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-1210</link>
		<dc:creator>Nicos Picos</dc:creator>
		<pubDate>Wed, 16 Jul 2008 15:04:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-1210</guid>
		<description>Hey,

I was wondering how come you don&#039;t use such helper methods for testing relationships. Just spent an entire day writing relationship tests, and would have been a lot easier to just have templates (even for the setup could be nice to have a template that will set up all your fixtures according to parameters). 

Any reason not to do this?</description>
		<content:encoded><![CDATA[<p>Hey,</p>
<p>I was wondering how come you don&#8217;t use such helper methods for testing relationships. Just spent an entire day writing relationship tests, and would have been a lot easier to just have templates (even for the setup could be nice to have a template that will set up all your fixtures according to parameters). </p>
<p>Any reason not to do this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Skoglund</title>
		<link>http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/comment-page-1/#comment-1089</link>
		<dc:creator>Kevin Skoglund</dc:creator>
		<pubDate>Tue, 08 Apr 2008 12:59:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.nullislove.com/2008/01/23/testing-in-rails-part-8-validations/#comment-1089</guid>
		<description>Good catch.  Thanks, David.</description>
		<content:encoded><![CDATA[<p>Good catch.  Thanks, David.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

