<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - Test Helper: Clean, Custom Assertion Messages]]></title>
		<link>http://railsforum.com/viewtopic.php?id=741</link>
		<description><![CDATA[The most recent posts in Test Helper: Clean, Custom Assertion Messages.]]></description>
		<lastBuildDate>Wed, 13 Jul 2011 06:08:46 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Test Helper: Clean, Custom Assertion Messages]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=142209#p142209</link>
			<description><![CDATA[<p>thanks for the link....very useful and informative just like the one I saw in <a href="http://www.webappmania.com/">Web Application Mania</a>..</p>]]></description>
			<author><![CDATA[dummy@example.com (brownie cookies9)]]></author>
			<pubDate>Wed, 13 Jul 2011 06:08:46 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=142209#p142209</guid>
		</item>
		<item>
			<title><![CDATA[Re: Test Helper: Clean, Custom Assertion Messages]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=58748#p58748</link>
			<description><![CDATA[<p>Enhancement: <a href="http://pragmatig.wordpress.com/2008/04/04/testing-validation-the-dry-way/">http://pragmatig.wordpress.com/2008/04/ &#133; e-dry-way/</a></p><p>working with a set of valid attributes, so that things like:<br />username should not be the same as password, password should match password_confirmation ... can be tested</p>]]></description>
			<author><![CDATA[dummy@example.com (grosser)]]></author>
			<pubDate>Fri, 04 Apr 2008 12:05:55 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=58748#p58748</guid>
		</item>
		<item>
			<title><![CDATA[Test Helper: Clean, Custom Assertion Messages]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=3791#p3791</link>
			<description><![CDATA[<p>In the <a href="http://railsforum.com/viewtopic.php?id=426">previous article</a> we ended up with this custom assertion:</p><p><pre name="code" class="ruby:nogutter">def assert_invalid_value(model_class, attribute, value)<br />&nbsp; if value.kind_of? Array<br />&nbsp; &nbsp; value.each { |v| assert_invalid_value model_class, attribute, v }<br />&nbsp; else<br />&nbsp; &nbsp; record = model_class.new(attribute =&gt; value)<br />&nbsp; &nbsp; assert !record.valid?, &quot;#{model_class} expected to be invalid when #{attribute} is #{value}&quot;<br />&nbsp; &nbsp; assert record.errors.invalid?(attribute), &quot;#{attribute} expected to be invalid when set to #{value}&quot;<br />&nbsp; end<br />end</pre><br />This works fine, but when the assertion fails we get this extra &quot;&lt;false&gt; is not true&quot; message at the end. Fore example:</p><p><pre name="code" class="ruby:nogutter">quantity expected to be invalid when set to -1.<br />&lt;false&gt; is not true.</pre><br />This second message is being generated by the call to &quot;assert&quot;:</p><p><pre name="code" class="ruby:nogutter">assert record.errors.invalid?(attribute), &quot;#{attribute} expected to be invalid when set to #{value}&quot;</pre><br />While checking out the Test::Unit documentation to see if there&#039;s some way to suppress this message, I noticed there&#039;s another method called <a href="http://www.ruby-doc.org/stdlib/libdoc/test/unit/rdoc/classes/Test/Unit/Assertions.html#M001763">assert_block</a> which all assertions are based upon. Hey! That sounds like exactly what we want. How it works is it raises an assertion with the given message if the block returns false. Let&#039;s try it out:</p><p><pre name="code" class="ruby:nogutter">assert_block &quot;#{attribute} expected to be invalid when set to #{value}&quot; do <br />&nbsp; record.errors.invalid? attribute<br />end</pre><br />Perfect, now we don&#039;t get that second error message.</p><p>If you notice, most assertion messages enclose the input variables in angled brackets &lt;like this&gt;. Let&#039;s do the same in our message and include the model name in there as&nbsp; well so it&#039;s a little more descriptive:</p><p><pre name="code" class="ruby:nogutter">assert_block &quot;&lt;#{model_class}.#{attribute}&gt; expected to be invalid when set to &lt;#{value}&gt;&quot; do <br />&nbsp; record.errors.invalid? attribute<br />end</pre><br />Finally, in our custom assertion we had two assertions, but the second one should catch anything the first one would have, so we can remove the first one. The end result looks like this:</p><p><pre name="code" class="ruby:nogutter">def assert_invalid_value(model_class, attribute, value)<br />&nbsp; if value.kind_of? Array<br />&nbsp; &nbsp; value.each { |v| assert_invalid_value model_class, attribute, v }<br />&nbsp; else<br />&nbsp; &nbsp; record = model_class.new(attribute =&gt; value)<br />&nbsp; &nbsp; assert_block &quot;&lt;#{model_class}.#{attribute}&gt; expected to be invalid when set to &lt;#{value}&gt;&quot; do<br />&nbsp; &nbsp; &nbsp; record.valid? # Must be called to generate the errors<br />&nbsp; &nbsp; &nbsp; record.errors.invalid? attribute<br />&nbsp; &nbsp; end<br />&nbsp; end<br />end</pre><br />And the error message now looks like this:</p><p><pre name="code" class="ruby:nogutter">&lt;LineItem.quantity&gt; expected to be invalid when set to &lt;-1&gt;</pre><br />Much better.</p><br /><p>In the previous article I had an idea for another custom assertion. Here it is:</p><p><pre name="code" class="ruby:nogutter">assert_invalid_attributes LineItem, :quantity =&gt; [-1, &#039;foo&#039;, 2.5], :product =&gt; nil</pre><br />This is surprisingly easy to make if we use our assert_invalid_value method. We just have to loop through the given hash and call that method for each entry. Here&#039;s the code:</p><p><pre name="code" class="ruby:nogutter">def assert_invalid_attributes(model_class, attributes)<br />&nbsp; attributes.each_pair do |attribute, value|<br />&nbsp; &nbsp; assert_invalid_value model_class, attribute, value<br />&nbsp; end<br />end</pre><br />Oh Ruby, how do I love thee? Let me count the ways...</p>]]></description>
			<author><![CDATA[dummy@example.com (ryanb)]]></author>
			<pubDate>Thu, 28 Sep 2006 16:42:05 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=3791#p3791</guid>
		</item>
	</channel>
</rss>
