title: Stubbing out ActiveRecord models in Service tests
body: <p>I'm following a TDD approach to building our app, and creating a whole bunch of service objects, keeping models strictly for data management.</p>
  
  <p>Many of the services I've built interface with models. Take for example MakePrintsForRunner:</p>
  
  <pre><code>class MakePrintsForRunner
  
    def initialize(runner)
      @runner = runner
    end
  
    def from_run_report(run_report)
      run_report.photos.each do |photo|
        Print.create(photo: photo, subject: @runner)
      end
    end
  
  end
  </code></pre>
  
  <p>I appreciate the create method could arguably be abstracted into the Print model, but let's keep it as is for now.</p>
  
  <p>Now, in the spec for MakePrintsForRunner I'm keen to avoid including spec_helper, since I want my service specs to be super fast.</p>
  
  <p>Instead, I stub out the Print class like this:</p>
  
  <pre><code>describe RunnerPhotos do
  
    let(:runner) { double }
    let(:photo_1) { double(id: 1) }
    let(:photo_2) { double(id: 2) }
    let(:run_report) { double(photos: [photo_1, photo_2]) }
  
    before(:each) do
      @service = RunnerPhotos.new(runner)
    end
  
    describe "#create_print_from_run_report(run_report)" do
  
      before(:each) do
        class Print; end
        allow(Print).to receive(:create)
        @service.create_print_from_run_report(run_report)
      end
  
      it "creates a print for every run report photo associating it with the runners" do
        expect(Print).to have_received(:create).with(photo: photo_1, subject: runner)
        expect(Print).to have_received(:create).with(photo: photo_2, subject: runner)
      end
    end
  
  end
  </code></pre>
  
  <p>And all goes green. Perfect!</p>
  
  <p>... Not so fast. When I run the whole test suite, depending on the seed order, I am now running into problems.</p>
  
  <p>It appears that the <code>class Print; end</code> line can sometimes overwrite <code>print.rb</code>'s definition of Print (which obviously inherits from ActiveRecord) and therefore fail a bunch of tests at various points in the suite. One example is:</p>
  
  <pre><code>NoMethodError:
    undefined method 'reflect_on_association' for Print:Class
  </code></pre>
  
  <p>This makes for an unhappy suite.</p>
  
  <p>Any advice on how to tackle this. While this is one example, there are numerous times where a service is directly referencing a model's method, and I've taken the above approach to stubbing them out. Is there a better way?</p>