title: Rspec Test validations of 'on: : update' not working (Rails4/Rspec 3)
body: <p>I have a model where I implemented a validate to prevent change after the initial value has been set (during the object creation).</p>
  
  <p>It works this way:</p>
  
  <p><strong>models/deal.rb</strong></p>
  
  <pre><code>validate :quantity_not_changeable, on: :update
  
  def quantity_not_changeable
        if quantity_changed?
          errors.add(:quantity,
           "Change of the initially defined qty is not possible")
        end
      end
  </code></pre>
  
  <p>This works in my app: I created a new Deal, it works. I try to edit it by changing the 'quantity' field, it fails. I try to edit another field (apart from 'quantity'), it works.
  so all is working.</p>
  
  <p><strong>But my Rspec Test to test it FAILS.</strong></p>
  
  <p>Actually <strong>I know it does NOT work because there is a problem with on: : validate.</strong></p>
  
  <p>Indeed WITHOUT the 'on: : validate', the test passes, but of course I can't just remove 'on: :update', as I only want the user not to be able to edit it after the initial creation of the Deal).</p>
  
  <pre><code>describe Deal do
  
    let(:admin_user) { FactoryGirl.create(:admin_user) }
  
    before(:each) do
      @attr = {
        title:                              "Neque porro quisquam est qui dolorem",
        description:                        "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum",
        quantity:  10
      }
    end
  
  describe "my test" do
  
   describe "test that nb of quantity can't be changed after initial creation" do    
      it "fails if initial quantity is changed" do 
        deal = Deal.create(@attr)
        deal.update(quantity: 15)     
        expect(deal).to have(1).errors_on(:quantity)
  
      end
    end
  </code></pre>
  
  <p>but I get this rspec error:</p>
  
  <pre><code>Failure/Error: expect(deal).to have(1).errors_on(:quantity)
         expected 1 errors on :quantity, got 0
  </code></pre>
  
  <p>I am going to use below "print variable": p Deal to show you WHY I am sure it is due to on: :update which is not working in TESTS.</p>
  
  <p>FIRST: if I place 'p deal' just before updating it, let's see the value of the deal object: </p>
  
  <pre><code>describe "my test" do
  
   describe "test that nb of quantity can't be changed after initial creation" do    
      it "fails if initial quantity is changed" do 
        deal = Deal.create(@attr)
        p deal
        deal.update(quantity: 15)     
        expect(deal).to have(1).errors_on(:quantity)
  
      end
    end
  </code></pre>
  
  <p>I get the following output (we see that all is normal: quantity is indeed 10)</p>
  
  <pre><code>#&lt;title: "Neque porro quisquam est qui dolorem", description: "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lo...", quantity: 10&gt;
  </code></pre>
  
  <p>SECOND: now let's move the 'p deal' AFTER my attempt in the test to update the value of 'quantity'</p>
  
  <pre><code>describe "my test" do
  
   describe "test that nb of quantity can't be changed after initial creation" do    
      it "fails if initial quantity is changed" do 
        deal = Deal.create(@attr)
        deal.update(quantity: 15)
        p deal     
        expect(deal).to have(1).errors_on(:quantity)
  
      end
    end
  </code></pre>
  
  <p>Now the output is:(<strong>we see that CONTRARY to what we could expect due to the validate on: :update in the code, the Rspec test does manage unfortunately to update the value of quantity</strong>):</p>
  
  <pre><code>#&lt;title: "Neque porro quisquam est qui dolorem", description: "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lo...", quantity: 15&gt;
  </code></pre>
  
  <p>So I don't understand why in reality, I tested manually and can not update, but in my rspec test suite it does update!!!</p>
  
  <p>Hours to understand it but to no success...I tried to find some online resources and some say <a href="http://www.dan-manges.com/blog/action-dependent-validations-and-why-on-update-is-bad" rel="nofollow">on: :update is a weak way to do things</a>. I really don't know what to do with this weird bug.</p>
  
  <p>Please help.</p>