Rspec:测试是否在作业中的对象上调用了方法? [英] Rspec: test if method was called on an object inside a job?

查看:69
本文介绍了Rspec:测试是否在作业中的对象上调用了方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 ended_at 小于今天,我会调用一个方法.出于某种原因,即使我在即将损坏的那一刻抛出绑定撬并手动调用该方法并且它工作正常,我也会不断失败.但是,如果我只是让规范测试自己运行,我仍然会失败.

I made a job to call a method if ended_at 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.

我的工作:

class RemoveOldEventsFromAlgoliaJob < ApplicationJob
  queue_as :default

  def perform

    old_events = Event.where("ended_at < ?", Date.today)
    old_events.each do |e|
      e.remove_from_algolia
    end
  end
end

我的方法:

def remove_from_algolia
  index = Algolia::Index.new(@@ALGOLIA_INDEX_NAME)
  index.delete_object(self.id)
end

我的规格测试:

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

RSpec 输出:

Failures:

  1) d will remove old events from the index
     Failure/Error: expect(event).to receive(:remove_from_algolia)

       (#<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>).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 <top (required)>'

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.

我可以在规范测试期间在我的工作中放入一个 binding.pry 并成功调用该方法:

I can put a binding.pry inside my job during the spec test and call that method successfully:

     4: def perform
     5:
     6:   old_events = Event.where("ended_at < ?", Date.today)
     7:   old_events.each do |e|
 =>  8:   binding.pry
     9:     e.remove_from_algolia
    10:   end
    11: end

[1] pry(#<RemoveOldEventsFromAlgoliaJob>)> e.remove_from_algolia
=> {"deletedAt"=>"2017-03-24T00:37:53.743Z", "taskID"=>206043962, "objectID"=>"409"}
[2] pry(#<RemoveOldEventsFromAlgoliaJob>)>

推荐答案

尝试使用 event 的存根代替真正的 Event 模型:

Try using a stub for event instead of a real Event model:

require 'rails_helper'

RSpec.describe RemoveOldEventsFromAlgoliaJob, type: :job do
    it "will remove old events from the index" do
    ActiveJob::Base.queue_adapter = :test
    event = double('event')
    allow(Event).to receive(:where).and_return([event])
    expect(event).to receive(:remove_from_algolia)

    RemoveOldEventsFromAlgoliaJob.perform_now

  end
end

这篇关于Rspec:测试是否在作业中的对象上调用了方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆