Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rspec:测试是否对作业中的对象调用了方法?_Ruby On Rails_Ruby_Rspec_Rails Activejob - Fatal编程技术网

Ruby on rails Rspec:测试是否对作业中的对象调用了方法?

Ruby on rails Rspec:测试是否对作业中的对象调用了方法?,ruby-on-rails,ruby,rspec,rails-activejob,Ruby On Rails,Ruby,Rspec,Rails Activejob,如果的结束时间小于今天,我会调用一个方法。出于某种原因,即使我在绑定撬即将中断的时候抛出了绑定撬,并手动调用该方法,但仍然会失败。然而,如果我只是让规范测试自己运行,我仍然会失败 我的工作: class RemoveOldEventsFromAlgoliaJob < ApplicationJob queue_as :default def perform old_events = Event.where("ended_at < ?", Date.today)

如果的结束时间小于今天,我会调用一个方法。出于某种原因,即使我在绑定撬即将中断的时候抛出了绑定撬,并手动调用该方法,但仍然会失败。然而,如果我只是让规范测试自己运行,我仍然会失败

我的工作:

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
我的规格测试:

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.

尝试使用
事件
的存根,而不是真正的
事件
模型:

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

当您手动调用该方法时,测试是通过还是仍然失败?一旦我手动调用它,然后退出
pry
,测试仍然失败。然而,我知道我正处于我想要的循环中,所以我不知道为什么我没有通过。我假设它可以工作,但是测试本身被破坏了。我认为这是测试是否对对象调用方法的正确方法。我以前没有做过RSpec模拟。在尝试您的方法后,您能分析此失败吗?`1) RemoveOldEventsFromAlgoliaJob将从索引中删除旧事件`
失败/错误:e.从\u algolia删除\u
\35;收到意外消息
:使用(无参数)从\u algolia删除\u
调用作业前移动最后一个预期,请参阅更新的代码。你有没有解释为什么这是有效的,但另一种方式却有效?我在这个问题上纠缠了一段时间,还没有找到这个解决方案。RSpec无法“记录”给定对象上发生的事情,除非它是由RSpec自己创建的。我确实质疑这样一个事实,即我并没有真正测试这个作业(因为它只调用比今天更老的事件。相反,我似乎只是在测试我的能力,看看对象是否可以接收该方法)。
     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>)>
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