<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: validations code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 10:29:58 GMT</pubDate>
    <description>DZone Snippets: validations code</description>
    <item>
      <title>Testing Rails Validations</title>
      <link>http://snippets.dzone.com/posts/show/1170</link>
      <description>&lt;code&gt;&lt;br /&gt;#&lt;br /&gt;# Useful for testing Rails ActiveRecord validations. For more information see:&lt;br /&gt;# http://wiseheartdesign.com/articles/2006/01/16/testing-rails-validations/&lt;br /&gt;#&lt;br /&gt;module ValidationTestHelper&lt;br /&gt;  def assert_valid(field, message, *values)&lt;br /&gt;    __model_check__&lt;br /&gt;    values.each do |value|&lt;br /&gt;      o = __setup_model__(field, value)&lt;br /&gt;      if o.valid?&lt;br /&gt;        assert_block { true }&lt;br /&gt;      else&lt;br /&gt;        messages = [o.errors[field]].flatten&lt;br /&gt;        assert_block("unexpected invalid field &lt;#{o.class}##{field}&gt;, value: &lt;#{value.inspect}&gt;, errors: &lt;#{o.errors[field].inspect}&gt;.") { false }&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def assert_invalid(field, message, *values)&lt;br /&gt;    __model_check__&lt;br /&gt;    values.each do |value|&lt;br /&gt;      o = __setup_model__(field, value)&lt;br /&gt;      if o.valid?&lt;br /&gt;        assert_block("field &lt;#{o.class}##{field}&gt; should be invalid for value &lt;#{value.inspect}&gt; with message &lt;#{message.inspect}&gt;") { false }&lt;br /&gt;      else&lt;br /&gt;        messages = [o.errors[field]].flatten&lt;br /&gt;        assert_block("field &lt;#{o.class}##{field}&gt; with value &lt;#{value.inspect}&gt; expected validation error &lt;#{message.inspect}&gt;, but got errors &lt;#{messages.inspect}&gt;") { messages.include?(message) }&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def __model_check__&lt;br /&gt;    raise "@model must be assigned in order to use validation assertions" if @model.nil?&lt;br /&gt;    &lt;br /&gt;    o = @model.dup&lt;br /&gt;    raise "@model must be valid before calling a validation assertion, instead @model contained the following errors #{o.errors.instance_variable_get('@errors').inspect}" unless o.valid?&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def __setup_model__(field, value)&lt;br /&gt;    o = @model.dup&lt;br /&gt;    attributes = o.instance_variable_get('@attributes')&lt;br /&gt;    o.instance_variable_set('@attributes', attributes.dup)&lt;br /&gt;    o.send("#{field}=", value)&lt;br /&gt;    o&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 17 Jan 2006 20:25:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1170</guid>
      <author>jlong (John W. Long)</author>
    </item>
  </channel>
</rss>
