我正在研究Rails API應用程序中的reset_password方法。當這個端點被擊中時,一個ActiveJob被排隊,將發出一個請求到Mandrill(我們的交易電子郵件客戶端)。我正在嘗試編寫測試以確保在控制器端點被擊中時ActiveJob已正確排隊。如何使用Rspec檢查在ActiveJob中排隊的內容
def reset_password
@user = User.find_by(email: params[:user][:email])
@user.send_reset_password_instructions
end
的send_reset_password_instructions創建它的代碼ActiveJob之前產生了一些網址等低於:
class SendEmailJob < ActiveJob::Base
queue_as :default
def perform(message)
mandrill = Mandrill::API.new
mandrill.messages.send_template "reset-password", [], message
rescue Mandrill::Error => e
puts "A mandrill error occurred: #{e.class} - #{e.message}"
raise
end
end
在我們沒有使用的ActiveJob任何適配器的時刻,所以我只是想檢查使用RSpec ActiveJob已排隊。
目前我的測試看起來是這樣的(我使用工廠女孩創建用戶):
require 'active_job/test_helper'
describe '#reset_password' do
let(:user) { create :user }
it 'should create an ActiveJob to send the reset password email' do
expect(enqueued_jobs.size).to eq 0
post :reset_password, user: { email: user.email }
expect(enqueued_jobs.size).to eq 1
end
end
一切都在現實的作品,我只需要創建測試!
我使用的是ruby 2.1.2和rails 4.1.6。
我看不到任何文檔或幫助網絡上的任何地方如何測試這個,所以任何幫助將不勝感激!
謝謝,這是一個很大的幫助。是有什麼方法可以檢查正確的作業排隊? – bobomoreno 2014-12-25 13:24:24
這應該是公認的答案。 – 2015-01-19 10:59:33
「ActiveJob :: TestHelper」ist旨在與minitest不使用rspec一起使用。它的代碼充滿了'assert_equal'等。只包含一個方法是壞主意,恕我直言。該模塊內的方法'enqueued_jobs'僅僅是'ActiveJob :: Base.queue_adapter.enqueued_jobs' – 2015-02-09 22:12:21