title: Rspec: test if method was called on an object inside a job?
body: <p>I made a job to call a method if <code>ended_at</code> was less than today. For some reason I keep getting failures even if I throw a binding pry right at the moment it is about to break and manually call the method and it works fine. However, I still get a failure if I just let the spec test run on its own. </p>
  
  <p>My job:</p>
  
  <pre><code>class RemoveOldEventsFromAlgoliaJob &lt; ApplicationJob
    queue_as :default
  
    def perform
  
      old_events = Event.where("ended_at &lt; ?", Date.today)
      old_events.each do |e|
        e.remove_from_algolia
      end
    end
  end
  </code></pre>
  
  <p>My method:</p>
  
  <pre><code>def remove_from_algolia
    index = Algolia::Index.new(@@ALGOLIA_INDEX_NAME)
    index.delete_object(self.id)
  end
  </code></pre>
  
  <p>My spec test:</p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe RemoveOldEventsFromAlgoliaJob, type: :job do
      it "will remove old events from the index" do
      ActiveJob::Base.queue_adapter = :test
      event = FactoryGirl.create(:event, title: "EXPIRED EVENT", ended_at: 1.day.ago)
  
      RemoveOldEventsFromAlgoliaJob.perform_now
  
      expect(event).to receive(:remove_from_algolia)
    end
  end
  </code></pre>
  
  <p>RSpec output:</p>
  
  <pre><code>Failures:
  
    1) d will remove old events from the index
       Failure/Error: expect(event).to receive(:remove_from_algolia)
  
         (#&lt;Event id: 401, uuid: "6e9a6f08-c34d-45af-9f3c-870b28643809", organization_id: nil, event_type_id: nil, name: "cool-event", title: "Cool Event", description: "Rad thing that's gonna happen", platform_type: "ee", platform_id: "12345678", platform_url: "http://event.com/12345678", featured: false, capacity: 100, rsvp_count: nil, attendee_count: nil, status: "upcoming", started_at: "2017-10-21 03:00:39", ended_at: "2017-03-23 07:00:00", deleted_at: "2017-10-21 03:00:39", created_at: "2017-03-24 00:24:54", updated_at: "2017-03-24 00:24:54", location_line_1: nil, location_line_2: nil, location_city: nil, location_state: nil, location_zip: nil, location_country: nil&gt;).remove_from_algolia(*(any args))
             expected: 1 time with any arguments
             received: 0 times with any arguments
       # ./spec/jobs/remove_old_events_from_algolia_job_spec.rb:10:in `block (2 levels) in &lt;top (required)&gt;'
  
  Finished in 0.8828 seconds (files took 7.12 seconds to load)
  1 example, 1 failure
  
  Failed examples:
  
  rspec ./spec/jobs/remove_old_events_from_algolia_job_spec.rb:4 # RemoveOldEventsFromAlgoliaJob will remove old events from the index
  
  Coverage report generated for RSpec to /workproject/coverage. 384 / 496 LOC (77.42%) covered.
  </code></pre>
  
  <p>I can put a <code>binding.pry</code> inside my job during the spec test and call that method successfully:</p>
  
  <pre><code>     4: def perform
       5:
       6:   old_events = Event.where("ended_at &lt; ?", Date.today)
       7:   old_events.each do |e|
   =&gt;  8:   binding.pry
       9:     e.remove_from_algolia
      10:   end
      11: end
  
  [1] pry(#&lt;RemoveOldEventsFromAlgoliaJob&gt;)&gt; e.remove_from_algolia
  =&gt; {"deletedAt"=&gt;"2017-03-24T00:37:53.743Z", "taskID"=&gt;206043962, "objectID"=&gt;"409"}
  [2] pry(#&lt;RemoveOldEventsFromAlgoliaJob&gt;)&gt;
  </code></pre>