我正在使用Rails Active Job对松弛通知进行排队。调度程序是sidekiq。一切正常,但不知道如何为Rails Active Job编写测试。我正在使用slack-notifier gem使用其Web挂钩与Slack连接。
在user.rb
def send_slack!
text = "New user #{first_name} #{last_name} has signed up"
channel = "#pltf_signup"
webhook_url = ENV["SLACK_SIGNUP_WEBHOOK_URL"]
NotifiableSlackJob.perform_later(webhook_url: webhook_url, text: text, channel: channel) if Rails.env.production?
end
在notifiable_slack_job.rb
class NotifiableSlackJob < ApplicationJob
queue_as :default
def perform(webhook_url:, text:, username: "BOT", channel:, icon_emoji: ":smile_cat:")
notifier = Slack::Notifier.new webhook_url, username: username, channel: channel, icon_emoji: icon_emoji
notifier.ping text
end
end
到目前为止,我写的测试是notifiable_slack_job_spec.rb
require 'rails_helper'
RSpec.describe NotifiableSlackJob, type: :job do
describe "#perform" do
it "sends a Slack message" do
NotifiableSlackJob.perform_now(webhook_url: ENV["SLACK_SIGNUP_WEBHOOK_URL"], text: "Hi", channel: '#pltf_signup')
end
end
end
我使用的测试框架是RSpec。 谢谢。