title: Rails 4, rspec 3: model validation test
body: <p>I have an <code>organization</code> object that has attributes <code>name, doing_business_as</code>.  I need to validate that the <code>name</code> is not the same as <code>doing_business_as</code>.</p>
  
  <pre><code># app/models/organization.rb
  class Organization &lt; ActiveRecord::Base
    validate :name_different_from_doing_business_as
  
    def name_different_from_doing_business_as
      if name == doing_business_as
        errors.add(:doing_business_as, "cannot be same as organization name")
      end
    end
  end
  </code></pre>
  
  <p>I have a matching rspec file that verifies this:</p>
  
  <pre><code># spec/models/organization_spec.rb
  require "rails_helper"
  
  describe Organization do
    it "does not allow NAME and DOING_BUSINESS_AS to be the same" do
      organization = build(:organization, name: "same-name", doing_business_as: "same-name")
  
      expect(organization.errors[:doing_business_as].size).to eq(1)
    end
  end
  </code></pre>
  
  <p>When I run the spec, however, it <em>fails</em> and this is what I get:</p>
  
  <pre><code>$ rspec spec/models/organization_spec.rb
  
  Organization
    does not allow NAME and DOING_BUSINESS_AS to be the same (FAILED - 1)
  
  Failures:
  
    1) Organization validations does not allow NAME and DOING_BUSINESS_AS to be the same
       Failure/Error: expect(organization.errors[:doing_business_as].size).to eq(1)
  
         expected: 1
              got: 0
  
         (compared using ==)
       # ./spec/models/organization_spec.rb:113:in `block (3 levels) in &lt;top (required)&gt;'
  
  Finished in 0.79734 seconds (files took 3.09 seconds to load)
  10 examples, 1 failure
  
  Failed examples:
  
  rspec ./spec/models/organization_spec.rb:110 # Organization validations does not allow NAME and DOING_BUSINESS_AS to be the same
  </code></pre>
  
  <p>I was expecting the spec to pass and ensure that the 2 attributes cannot be the same. In the Rails console I can mimic the expected behavior, but I can't seem to get the spec to "fail" successfully.</p>
  
  <p>I also checked via the Rails Console that it works as expected:</p>
  
  <pre><code>$ rails c
  &gt; o = Organization.new(name: "same", doing_business_as: "same")
  &gt; o.valid?
    =&gt; false
  &gt; o.errors[:doing_business_as]
    =&gt; ["cannot be the same as organization name"]
  </code></pre>
  
  <p>So I know the functionality is there, but I can't get a workable test...</p>