title: Stubbing validation on an ActiveRecord across the RSpec test suite
body: <p>I have an ActiveRecord object which gets created/saved in approximately 50% of my RSpec suite.
  I want to stub a validation of it across the suite apart from the context where I am explicitly testing the behaviour of validation.</p>
  
  <p>To achieve this, I stubbed it globally using config</p>
  
  <pre><code>config.before(:each) do
    allow_any_instance_of(Activerecord::Abc).to receive(:abc_validate).and_return(true)
  end
  </code></pre>
  
  <p>and then overwrote the stub in the specific context</p>
  
  <pre><code>context "Validations" do
    context "abc_validate" do
      before :each do
        allow_any_instance_of(Activerecord::Abc).to receive(:abc_validate).and_call_original
      end
  
      it "should ..... " do
        ........
      end
    end
  end
  </code></pre>
  
  <p>This meets my requirement. However, it is not efficient, as, although I need the stub only for approximately 50% of my RSpec test suite, I am actually stubbing it for every unit test. Even the unit tests which don't need the stub will have a stubbed function.</p>
  
  <p>Is there a better way to do it?</p>
  
  <p>I thought of one another approach: Instead of stubbing the method, I can re-open the class in RSpec and over-write the custom-validation function itself.</p>
  
  <pre><code>Activerecord::Abc.class_eval do
    def abc_validate(testing_self = false)
      testing_self ? super : true
    end
  end
  </code></pre>
  
  <p>However, this approach feels too hacky.</p>
  
  <p>Version details -</p>
  
  <ol>
  <li>Rails 4.2.4</li>
  <li>Rspec 3.3.0</li>
  </ol>