[英]When testing a rake task, why does rspec receive this method twice when it is only called once?
我正在嘗試測試rake任務,並得到一個非常奇怪的結果。
expected: 1 time with arguments:
received: 2 times with arguments:
有問題的代碼僅調用該方法一次,而我無法確定多余的調用來自何處。
這是我第一次為rake任務編寫測試,因此我可能忽略了一些顯而易見的事情。 以下內容看起來是否合理?
#spec/lib/scheduled_tasks_spec.rb
require 'rails_helper'
require 'rake'
MyApp::Application.load_tasks
load File.join(Rails.root, 'lib', 'tasks', 'scheduled_tasks.rake')
RSpec.describe "scheduled rake tasks" do
describe 'Initialization' do
let!(:team) { create :team }
let!(:admin) { create :user, role: 'admin' }
it 'should call the GeneratePdf job for each team' do
expect(GenerateReportJob).to(
receive(:perform_later).with(
[admin], team
).once.and_call_original
)
Rake::Task['scheduled_tasks:generate_reports'].invoke
end
end
end
我正在測試的rake任務非常簡單
namespace :scheduled_tasks do
task generate_reports: :environment do
admins = Admin.where(role: 'admin')
# byebug
Team.all.each do |team|
GenerateReportJob.perform_later(admins, team)
end
end
end
您在這里兩次加載了任務定義。
MyApp::Application.load_tasks
load File.join(Rails.root, 'lib', 'tasks', 'scheduled_tasks.rake')
顯然,這導致名稱"scheduled_tasks:generate_reports"
指向兩個 [相同]代碼塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.